简体   繁体   English

处理CSV单元格值中的逗号和引号

[英]handle commas and quotes in cell values of CSV

I am using python's csv reader but some columns are failing to process because they contain a comma or quote. 我正在使用python的csv阅读器,但某些列由于包含逗号或引号而无法处理。

How can you get the correct string? 如何获得正确的字符串?

For example one cell says: 例如,一个单元格说:

"['one', 'two', 'three']"

but it gives me: 但这给了我:

"['one'

if os.path.exists(CSV_PATH):
    with open(CSV_PATH, 'r') as csv_file:
        reader = csv.reader(csv_file, delimiter=',', quotechar='|')
        field_list = []
        for row in reader:
            if not field_list:
                field_list = [c for c in row]
                continue

            d = dict.fromkeys(field_list)
            for header, col in zip(field_list, row):
                print(col)  

You can use escapechar attribute with some prefix symbol: 您可以使用带有一些前缀符号的escapechar属性:

A one-character string used by the writer to escape the delimiter if quoting is set to QUOTE_NONE and the quotechar if doublequote is False. 如果将引号设置为QUOTE_NONE,则编写器使用一个单字符字符串来转义分隔符,如果双引号设置为False,则使用quotechar。 On reading, the escapechar removes any special meaning from the following character. 阅读时,escapechar删除了以下字符的任何特殊含义。 It defaults to None, which disables escaping. 它默认为无,这将禁用转义。

Also you can use doublequote attribute with " symbol: 您也可以将doublequote属性与"符号一起使用:

Controls how instances of quotechar appearing inside a field should themselves be quoted. 控制出现在字段中的quotechar实例本身应如何被引用。 When True, the character is doubled. 当为True时,字符加倍。 When False, the escapechar is used as a prefix to the quotechar. 如果为False,则将escapechar用作quotechar的前缀。 It defaults to True. 默认为True。

On output, if doublequote is False and no escapechar is set, Error is raised if a quotechar is found in a field. 在输出上,如果双引号为False且未设置转义符,则如果在字段中找到quotechar,则会引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM