I'd like to distinguishing None and empty strings when going back and forth between python data structure and csv representation using python's csv module.
My issue is that when I run
import csv, cStringIO
data = [['NULL/None value',None],
['empty string','']]
f = cStringIO.StringIO()
csv.writer(f).writerows(data)
f = cStringIO.StringIO(f.getvalue())
data2 = [e for e in csv.reader(f)]
print "input : ", data
print "ouput : ", data2
I get the following output :
input : [['NULL/None value', None], ['empty string', '']]
ouput : [['NULL/None value', ''], ['empty string', '']]
Of course, I could play with data and data2 to distinguish None and empty string with things like
data =[d if d!=None else 'None' for d in data]
data2 =[d if d!='None' else None for d in data2]
But that would partly defeat the interest of the csv module (quick deserialization/serialization implemented in c, specially when you are dealing with large lists).
Is there a csv.Dialect or parameters to csv.writer, csv.reader that would enable to distinguish between '' and None in this use-case ?
If not, would there be an interest in implementing a patch to csv writer to enable this kind of back and forth ? (Possibly a Dialect.None_translate_to parameter defaulting to '' to ensure backward compatibility)