Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-08
5
Medium Priority
?
108 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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

782 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