【发布时间】:2021-01-21 15:54:26
【问题描述】:
我有一个 csv 文件,其中包含以下行:
question|"N|1|2|3|4|4|30|2|"N|full_answer
为了阅读文件,我会这样做:
with open('questions.csv', 'r') as question_file:
questions = csv.reader(question_file, delimiter='|', doublequote=False, skipinitialspace=True)
for line in questions:
print(line)
print的输出是:
['question', 'N|1|2|3|4|4|30|2|N', 'full_answer']
但预期的输出是:
['question', '"N', '1', '2', '3', '4', '4', '30', '2', '"N', 'full_answer']
任何忽略双引号的解决方案?
【问题讨论】:
-
如果将
doublequote=False更改为True会怎样? -
@mkrieger1 输出是一样的。
-
docs.python.org/3/library/csv.html#csv.Dialect.doublequote
doublequote=True不会按照你的想法去做。 -
@mkrieger1 感谢您在 SA 上为我找到一篇出色的帖子,但我正在寻找一种官方方法来做到这一点,而不是一种 hack。
-
我还注意到它在问一些不同的东西,没关系。
标签: python python-3.x list csv parsing