Solved

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

Posted on 2016-09-08
5
65 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 250 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 250 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

636 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