[张贴编辑更清楚。]
如果您不想在双引号下解析逗号,那么您的输出将包括列内的逗号,这是另一种执行此操作的方法。它很优雅,允许您使用云存储桶来存储您的 CSV 文件。关键是使用smart_open 作为标准文件打开的替代品。
另外,我使用的是DictReader 而不是阅读器。
import csv
import json
from smart_open import open
with open('./temp.csv') as csvFileObj:
reader = csv.DictReader(csvFileObj, delimiter=',', quotechar='"')
# csv.reader requires bytestring input in python2, unicode input in python3
for record in reader:
# record is a dictionary of the csv record
print(f'Record as json shows proper reading of file:\n {json.dumps(record, indent=4)})')
print(f'You can reference an individual field too: {record["field3"]}')
print(f' {record["field4"]}')
请注意,我向 DictReader 添加了 2 个参数。
分隔符=',',quotechar='"'
逗号是默认分隔符,但我添加了它以防有人需要更改它。 Quotechar 是必需的,因为它不是默认值。
代码的实际输出:
Record as json shows proper reading of file:
{
"field1": "AAA",
"field2": "BBB",
"field3": "Test, Test",
"field4": "CCC"
})
You can reference an individual field too: Test, Test
CCC
done
Record as json shows proper reading of file:
{
"field1": "111",
"field2": "222, 333",
"field3": "XXX",
"field4": "YYY, ZZZ"
})
You can reference an individual field too: XXX
YYY, ZZZInput file:
输入数据文件(为了清楚起见,我添加了一个标题记录。如果您没有标题记录,第一个记录将被吞噬,但也有可能的参数。)
"field1","field2","field3","field4"
"AAA","BBB","Test, Test","CCC"
"111","222, 333","XXX","YYY, ZZZ"