Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Python 2.7 - replace() - add "" to empty variable

Posted on 2016-09-08
5
Medium Priority
?
92 Views
Last Modified: 2016-09-17
Hi there,

Consider this: item['phone'] = phone

Using Python, I need to surround the variable phone with " when it's value is empty.

For example, if phone is empty, then phone = ""

Thanks for your help,
Rene
0
Comment
Question by:ReneGe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 29

Accepted Solution

by:
pepr earned 1000 total points
ID: 41791071
Can you explain better why you want to make the empty string the non-empty string, and why a non-empty should not also be enclosed in double quotes? Are you generating a text file? Or do you want to wrap the string for displaying?
0
 
LVL 51

Assisted Solution

by:Steve Bink
Steve Bink earned 1000 total points
ID: 41791735
I agree with pepr's implication - this should be part of the view, not part of the model.  You should be storing the original input verbatim to preserve integrity.  If you would like it to appear in a particular fashion for reporting or UI purposes, implement the formatting on output.
0
 
LVL 10

Author Comment

by:ReneGe
ID: 41791749
I also agree with you both.

I could have added " to all the fields, that would still do.  But that will be another question.

My problem was that I needed to manage the csv file later on by using a batch file; where empty fields are completely discarded.

I resolved my problem in my batch file by temporary adding ,"", in the empty fields and by only having the comma as the delimiter.  For later on, removing "" in the FOR /F loop by adding ~ (%%~A).

Thanks for your responses.  You help was greatly appreciated.
0
 
LVL 10

Author Comment

by:ReneGe
ID: 41791751
Thanks for all your help :)
Cheers mates!
0
 
LVL 10

Author Closing Comment

by:ReneGe
ID: 41803422
I though this one was already closed.  Sorry about that. Thanks again for all your help.  Cheers mates
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question