首先,我假设CSV文件在字符串值周围有引号。在
因此,每个包含逗号的值都被这样的引号包围:a, b, 2, 'foo, bar'
如果是,则可以使用以下方法:
进口csv
quoteChar = "'"
myReader = csv.reader(open('largefile.csv', 'rb'), delimiter=',', quoteChar = quoteChar)
for row in myReader:
print row # this prints a list. Row is a list of values per line in your
# 'largefile.csv' file. Each row is splitted on the delimiter char.
# (comma in this case) and strings that are quoted by quoteChar (in your case
# a ' are treated differently.) This will work for your input that you've given
# in your question.
# this is what you used to do:
for line in open('largefile.csv', 'rb'):
row = line.split(quoteChar)
如果不是这样的话,那就要看你的CSV文件是什么样子了。在