Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

csv ignore comma inside double quote

Tags:

python

csv

"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)
like image 560
xuanyue Avatar asked Oct 27 '25 16:10

xuanyue


2 Answers

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

csv.reader(f, doublequote=True, quoting=csv.QUOTE_ALL, escapechar='\\')
like image 188
tdelaney Avatar answered Oct 30 '25 07:10

tdelaney


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).

like image 38
Danny_ds Avatar answered Oct 30 '25 08:10

Danny_ds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!