Split string on commas but ignore commas within double-quotes?

扶醉桌前 提交于 2019-11-26 07:44:04

问题


I have some input that looks like the following:

A,B,C,\"D12121\",E,F,G,H,\"I9,I8\",J,K

The comma-separated values can be in any order. I\'d like to split the string on commas; however, in the case where something is inside double quotation marks, I need it to both ignore commas and strip out the quotation marks (if possible). So basically, the output would be this list of strings:

[\'A\', \'B\', \'C\', \'D12121\', \'E\', \'F\', \'G\', \'H\', \'I9,I8\', \'J\', \'K\']

I\'ve had a look at some other answers, and I\'m thinking a regular expression would be best, but I\'m terrible at coming up with them.


回答1:


Lasse is right; it's a comma separated value file, so you should use the csv module. A brief example:

from csv import reader

# test
infile = ['A,B,C,"D12121",E,F,G,H,"I9,I8",J,K']
# real is probably like
# infile = open('filename', 'r')
# or use 'with open(...) as infile:' and indent the rest

for line in reader(infile):
    print line
# for the test input, prints
# ['A', 'B', 'C', 'D12121', 'E', 'F', 'G', 'H', 'I9,I8', 'J', 'K']


来源:https://stackoverflow.com/questions/8069975/split-string-on-commas-but-ignore-commas-within-double-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!