csv Parser Question - Handling of Double Quotes

jwbrown77 at gmail.com jwbrown77 at gmail.com
Thu Mar 27 15:41:56 EDT 2008


Hello,

I am trying to read a csv file.  I have the following functioning
code:

---- BEGIN ----
import csv

reader = csv.reader(open("test.csv", "rb"), delimiter=';')

for row in reader:
  print row
---- END ----

This code will successfully parse my csv file formatted as such:

"this";"is";"a";"test"

Resulting in an output of:

['this', 'is', 'a', 'test']

However, if I modify the csv to:

"t"h"is";"is";"a";"test"

The output changes to:

['th"is"', 'is', 'a', 'test']

My question is, can you change the behavior of the parser to only
remove quotes when they are next to the delimiter?  I would like both
quotes around the h in the example above to remain, however it is
instead removing only the first two instances of quotes it runs across
and leaves the others.

The closest solution I have found is to add to the reader command
"escapechar='\\'" then manually add a single \ character before the
quotes I'd like to keep.  But instead of writing something to add
those slashes before csv parsing I was wondering if the parser can
handle it instead.

Thanks in advance for the help.



More information about the Python-list mailing list