Why is the Python CSV reader ignoring double-quoted fields?

If you look at the dialect that you’re using, you’ll notice that the excel dialect is
configured as follows:

class excel(Dialect):
    """Describe the usual properties of Excel-generated CSV files."""
    delimiter=","
    quotechar="""
    doublequote = True
    skipinitialspace = False
    lineterminator="\r\n"
    quoting = QUOTE_MINIMAL

Notice that skipinitialspace is set to False. Just pass that into your reader.
Oh and by the way, all the fields you’ve passed in are already the defaults when
using the excel dialect, which is the default dialect parameter passed to csv.reader

So, I would re-write your code like so:

>>> with open(inPath) as fp:
>>>     reader = csv.reader(fp, skipinitialspace=True)
>>>     for row in reader:
>>>         print row,
>>>         print len(row)
['hello', 'this is row 1', 'foo1'] 3
['hello', 'this is row 2', 'foo2'] 3
['goodbye', 'this, is row 3', 'foo3'] 3

Leave a Comment