Back
How can I do the following in Python?
row = [unicode(x.strip()) for x in row if x is not None else '']
Essentially:
replace all the Nones with empty strings, and then
carry out a function.
For using if/else in a list comprehension you can use the below-mentioned code:-
def change(f): if f is None: return unicode(f.strip()) else: return '' row = [change(x) for x in row]
def change(f):
if f is None:
return unicode(f.strip())
else:
return '' row = [change(x) for x in row]
You can try using the code given below:
def change(f): if f is None: return unicode(f.strip()) else: return ''row = [change(x) for x in row]
return ''
row = [change(x) for x in row]
Although then you have:
row = map(change, row)
Or you can use a lambda inline.
31k questions
32.8k answers
501 comments
693 users