简体   繁体   中英

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)

只需添加一个转义字符来处理csv中的转义引号

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM