Python解析CSV忽略带双引号的逗号

时间:2014-02-03 12:08:58

标签: python python-2.7 csv

我有一个包含以下行的CSV文件:

"AAA", "BBB", "Test, Test", "CCC"
"111", "222, 333", "XXX", "YYY, ZZZ" 

依旧......

我不想用双引号解析逗号。即。我的预期结果应该是

AAA
BBB
Test, Test
CCC

我的代码:

import csv
with open('values.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print row

我尝试在python下使用csv包但没有运气。解析会爆炸所有逗号。

如果我遗失了某些内容,请告诉我

2 个答案:

答案 0 :(得分:38)

这应该做:

lines = '''"AAA", "BBB", "Test, Test", "CCC"
           "111", "222, 333", "XXX", "YYY, ZZZ"'''.splitlines()
for l in  csv.reader(lines, quotechar='"', delimiter=',',
                     quoting=csv.QUOTE_ALL, skipinitialspace=True):
    print l
>>> ['AAA', 'BBB', 'Test, Test', 'CCC']
>>> ['111', '222, 333', 'XXX', 'YYY, ZZZ']

答案 1 :(得分:12)

输入中的引号字符前面有空格。设置skipinitialspace to True以跳过分隔符后的任何空格:

  

True时,忽略紧跟在分隔符之后的空格。默认值为False

>>> import csv
>>> lines = '''\
... "AAA", "BBB", "Test, Test", "CCC"
... "111", "222, 333", "XXX", "YYY, ZZZ" 
... '''
>>> reader = csv.reader(lines.splitlines())
>>> next(reader)
['AAA', ' "BBB"', ' "Test', ' Test"', ' "CCC"']
>>> reader = csv.reader(lines.splitlines(), skipinitialspace=True)
>>> next(reader)
['AAA', 'BBB', 'Test, Test', 'CCC']