Tregoreg
Tregoreg

Reputation: 22286

Handling escaped quotes with Python's csv.reader

Using python's csv module, I'm trying to read some CSV data.

I'm using the code:

dialect = csv.Sniffer().sniff(csv_file.read(1024))
csv_file.seek(0)
reader = csv.reader(csv_file, dialect)

for line in reader:
    ...

Everything works fine except for lines containing escaped quotes:

11837,2,NULL,"\"The Take Over, The Breaks Over\"","Fall Out Boy"

Such a line is tokenized as:

['11837', '2', 'NULL', '\\The Take Over', ' The Breaks Over\\""', 'Fall Out Boy']

The dialect contains the following properties:

dialect.quotechar = "
dialect.quoting = 0
dialect.escapechar = None
dialect.delimiter = ,
dialect.doublequote = False
dialect.lineterminator = \n

Is there anything I can do besides writing my own CSV parser?

Upvotes: 6

Views: 7006

Answers (1)

cwallenpoole
cwallenpoole

Reputation: 82078

If I'm not mistaken, dialect.escapechar = None should be dialect.escapechar = '\\'

If you look at the examples in the docs, it certainly seems to suggest making that alteration

Upvotes: 6

Related Questions