Solved

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

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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.
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…
The viewer will learn how to count occurrences of each item in an array.

710 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