Read CSV file with comma within fields in Python

The Python csv module actually does support quoted fields, even by default. Your problem here is that Python by default does not skip the space, so you need to use skipinitialspace=True.

>>> s = StringIO.StringIO('1, "text1,text2", "text3, text4", a, b, c')
>>> list(csv.reader(s, skipinitialspace=True))
[['1', 'text1,text2', 'text3, text4', 'a', 'b', 'c']]

Leave a Comment