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

Viewed 32380

I think this is probably something simple, but after an hour of searching, I've had no luck figuring out what I'm doing wrong.

I'm using the following code to read a CSV file - I have no problem reading the file, but when a line contains a field that is double-quoted because it contains the delimiter, the CSV reader ignores the double-quotes and parses the field into 2 separate fields.

Here's the code I'm using:

myReader = csv.reader(open(inPath, 'r'), dialect='excel', delimiter=',', quotechar='"')
for row in myReader:
    print row,
    print len(row)

My input:

hello, this is row 1, foo1
hello, this is row 2, foo2
goodbye, "this, is row 3", foo3

Which gives me:

['hello', ' this is row 1', ' foo1'] 3
['hello', ' this is row 2', ' foo2'] 3
['goodbye', ' "this', ' is row 3"', ' foo3'] 4

What do I need to change so it will recognize the double-quoted field as one field? I'm using python version 2.6.1.

Thanks!

2 Answers
Related