xuanyue
xuanyue

Reputation: 1428

csv ignore comma inside double quote

"Medical Center Emergency Physicians \"North Shore\"  Houston, TX  EM  MD-DO",2680,18882

I want it to be read in as length 3 list use python csv module this my expected output

["Medical Center Emergency Physicians \"North Shore\" Houston, TX EM MD-DO", '2680', 18882]

I have tried a lot use different parameter. But all the below I tried doesn't work for me. All of them output a length 4 list. I think this is caused by the comma after Huston. But how can we ignore it since it is in a double quote?

csv_reader = csv.reader(f, doublequote=True, quoting=csv.QUOTE_ALL)
csv_reader = csv.reader(f, doublequote=False, quoting=csv.QUOTE_ALL)
csv_reader = csv.reader(f, doublequote=False)
csv_reader = csv.reader(f, doublequote=True)
csv_reader = csv.reader(f)

Upvotes: 4

Views: 6311

Answers (2)

Danny_ds
Danny_ds

Reputation: 11406

You'll have to use:

csv.reader(f, quotechar='"')

and possibly some parameter to tell the reader the quotes are escaped with \. But if your current output is 4 fields, it seems to split on the ,, ignoring the \".

Most likely like this:

csv.reader(f, quotechar='"', escapechar='\\')

Those \ shouldn't be in your output (unless you need them for further processing).

Upvotes: 2

tdelaney
tdelaney

Reputation: 77337

Just add an escape character to deal with escaped quotes in the csv

csv.reader(f, doublequote=True, quoting=csv.QUOTE_ALL, escapechar='\\')

Upvotes: 2

Related Questions