Solved

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

Posted on 2016-09-08
5
34 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 28

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 50

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now