且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在逗号上拆分字符串,但是在双引号中忽略逗号?

更新时间:2023-02-21 12:38:27

Lasse是对的;它是一个逗号分隔的值文件,因此您应该使用 csv 模块。一个简单的例子:

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']