string % dictionary question

J

Jorge Godoy

Sam Sungshik Kong said:
Hello, group!

di={}
di["test"]=None
s="%(test)s" % di
s
'None'
</code>

I want the result to be just empty string when the dictionary value is None.
Is there a good way?

You can check it after the definition and make the necessary changes...
di={}
di["test"]=None
s="%(test)s" % di
if s == "None": s = "" ....
s ''
di["test"] = 1
s = "%(test)s" % di
if s == "None": s = "" ....
s '1'


Be seeing you,
 
S

Sam Sungshik Kong

Thanks for the reply.

What if the string is more complicated?
For example,

di = {}
di["name"] = "Sam"
di["age"] = None
s = "name: %(name)s, age: %(age)d" % di

Thanks.

Sam

Jorge Godoy said:
Sam Sungshik Kong said:
Hello, group!

di={}
di["test"]=None
s="%(test)s" % di
s
'None'
</code>

I want the result to be just empty string when the dictionary value is None.
Is there a good way?

You can check it after the definition and make the necessary changes...
di={}
di["test"]=None
s="%(test)s" % di
if s == "None": s = "" ...
s ''
di["test"] = 1
s = "%(test)s" % di
if s == "None": s = "" ...
s '1'


Be seeing you,
 
B

Bryan

Sam said:
Thanks for the reply.

What if the string is more complicated?
For example,

di = {}
di["name"] = "Sam"
di["age"] = None
s = "name: %(name)s, age: %(age)d" % di

Thanks.

Sam

instead of setting di['age'] to None, set it to empty string. if you are setting di['age'] with a variable then you can
insure that you use an empty string for None by doing something like this:


di['age'] = somevar or ''



bryan
 
E

Erik Max Francis

Sam said:
I want the result to be just empty string when the dictionary value is
None.
Is there a good way?

Best way is either subclass dict or write a simple wrapper object which
returns "" instead of None. Or use EmPy :).
 
A

Alex Martelli

Sam Sungshik Kong said:
Hello, group!

di={}
di["test"]=None
s="%(test)s" % di
s
'None'
</code>

I want the result to be just empty string when the dictionary value is None.
Is there a good way?

class Translator(object):
def __init__(self, d, t=''):
self.d = d
self.t = t
def __getitem__(self, n):
result = self.d.get(n)
if result is None: result = self.t
return result

s = 'before%(test)safter' % Translator({})

will leave 'beforeafter' in s, as the missing key gets treated just like
a None. If you want this to raise, and only explicit None values to get
translated, change the first line of __getitem__ into
'result=self.d[n]'.


Alex
 
L

Larry Bates

di={}
di["test"]=None
s="%s" % (di["test"] or "")
print "s='%s'" % s

works, but might be better to set values to empty strings
unless you want to test for the existence of None somewhere
else in your program.

Larry Bates

Sam Sungshik Kong said:
Hello, group!

di={}
di["test"]=None
s="%(test)s" % di
s
'None'
</code>

I want the result to be just empty string when the dictionary value is None.
Is there a good way?

TIA.
Sam
 
S

Sam Sungshik Kong

Thanks Larry!

What about numbers?

s="%d" % di["test"]

di["test"] could be a number or None.

What can I do with that?
"" is not appropriate, right?

Sam

Larry Bates said:
di={}
di["test"]=None
s="%s" % (di["test"] or "")
print "s='%s'" % s

works, but might be better to set values to empty strings
unless you want to test for the existence of None somewhere
else in your program.

Larry Bates

Sam Sungshik Kong said:
Hello, group!

di={}
di["test"]=None
s="%(test)s" % di
s
'None'
</code>

I want the result to be just empty string when the dictionary value is None.
Is there a good way?

TIA.
Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top