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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21527057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:04:54  来源:igfitidea点击:

Python parse CSV ignoring comma with double-quotes

pythonpython-2.7csv

提问by Abhiram Sampath

I have a CSV file with lines like this:

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

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

and so on ...

等等 ...

I dont want to parse comma's under double-quotes. ie. My expected result should be

我不想在双引号下解析逗号。IE。我的预期结果应该是

AAA
BBB
Test, Test
CCC

My code:

我的代码:

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

I tried using csv package under python but no luck. The parses explodes all comma's.

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

Please let me know if I'm missing something

如果我遗漏了什么,请告诉我

采纳答案by Michael

This should do:

这应该做:

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

回答by Martijn Pieters

You have spaces before the quote characters in your input. Set skipinitialspaceto Trueto skip any whitespace following a delimiter:

您输入的引号字符前有空格。设置skipinitialspaceTrue跳过分隔符后的任何空格:

When True, whitespace immediately following the delimiteris ignored. The default is False.

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