网站上的CSV格式数据未正确解析

时间:2012-01-04 20:25:58

标签: python csv

我的Python脚本通过urllib2打开一个文件;数据如下所示:

"Charitable Donation: Contribution","012","","","","","","","","","","","","","","","","","","","",""

Python脚本:

reader = csv.reader(data, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
for row in reader:
    print row

输出是这样的:

['Charitable Donation: Contribution']
['', '']
['012']
['', '']
['']
['', '']
['']
['', '']
['']
['', '']
['']
['', '']
['']
['', '']
['']
['', '']
['']
['', '']
['']
['', '']
['']

我做错了什么?

4 个答案:

答案 0 :(得分:3)

你使用双引号(")作为分隔符而不是逗号(,)... 为什么?怎么样

reader = csv.reader(data, delimiter=',', quoting=csv.QUOTE_ALL)
for row in reader:
    print row

# you can omit the delimiter argument as well, since comma is the default:
reader = csv.reader(data, quoting=csv.QUOTE_ALL)
# etc.

答案 1 :(得分:2)

您希望逗号是分隔符,因为分隔符是分隔值的内容。 quotechar是“因为它包含的是什么值。

尝试

reader = csv.reader(data, delimiter=',',quotechar='"', quoting=csv.QUOTE_ALL)

答案 2 :(得分:2)

第一个参数是迭代器。在示例here中,它们使用文件对象。迭代一个字符串会给出每个字符,但是你只想要这一行。最简单的解决方法是使data成为一个列表。

reader = csv.reader([data])
for row in reader:
    print row

它给出了输出:

['Charitable Donation: Contribution', '012', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

答案 3 :(得分:1)

我认为你使用了错误的分隔符 - 你应该使用','然后注意你引用的部分被引用的事实。