在csv中,对于列,字符串中存在歧义。因此,我在列表中获得6
值,而不是5
值作为输出。
代码:
import csv
csv_data = csv.reader(file('test.csv'))
for row in csv_data:
print row
我尝试用space
替换"
,以获得至少与不带引号的普通字符串相同的结果,如下所示:
for row in csv_data:
print [r.replace('"',' ') for r in row] # This did't worked as expected.
输入:
csv文件中的行看起来像这样,
1,2,"text1", "Sample text ""present" in csv, as this",5
"Sample text "present" in csv, as this" # Error due to this value.
输出:
['1', '2', 'text1', 'Sample text present" in csv', 'as this', 5]
预期输出:
['1', '2', 'text1', 'Sample text "present" in csv, as this', 5]
1条答案
按热度按时间r7s23pms1#
这几乎是令人尴尬的黑客,但似乎至少在你的问题中显示的示例输入上有效。它的工作原理是对
csvreader
读取的每一行进行后处理,并尝试检测它们何时由于格式错误而被错误读取,然后进行纠正。输出: