csv reader behavior with None and empty string

后端 未结 7 1476
夕颜
夕颜 2020-12-01 13:59

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

7条回答
  •  失恋的感觉
    2020-12-01 14:36

    I don't think it would be possible to do what you want with a mere dialect, but you could write your own csv.reader/write subclass. On the other hand, I still think that is overkill for this use case. Even if you want to catch more than just None, you probably just want str():

    >>> data = [['NULL/None value',None],['empty string','']]
    >>> i = cStringIO.StringIO()
    >>> csv.writer(i).writerows(map(str,row) for row in data)
    >>> print i.getvalue()
    NULL/None value,None
    empty string,
    

提交回复
热议问题