0

我在 python 中读取 csv 文件时遇到了一些问题我有我的数据,但有些列里面有双引号,例如:

First field First Row,This is the second field in the first row
First Field Second Row,This is the "second" field in the second row

所以,我的 csv 阅读器如下:

with open('data.csv', encoding="utf-8") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')

问题是,当我遍历行时​​,当它检查第二行时,由于某种原因它不会在“,”上拆分列。所以当我打印每一行时我得到了这个:

['First field First Row' , 'This is the second field in the first row']
['First Field Second Row,This is the "second" field in the second row']

有什么解决方法可以正确拆分吗?

提前致谢!

4

1 回答 1

0

默认情况下,双引号是特殊的,用于引用字段。您必须告诉读者它们在您的文件中并不特殊:

with open('data.csv', encoding="utf-8") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',', quoting=csv.QUOTE_NONE)
于 2020-04-16T14:15:45.973 回答