在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]
这几乎是令人尴尬的黑客,但似乎至少工作在您的问题中显示的样本输入。它的工作原理是对
csvreader
读取的每一行进行后处理,并尝试检测它们是否由于格式错误而被错误读取,然后进行纠正。你知道吗输出:
相关问题 更多 >
编程相关推荐