使用以下代码:
import sys
import os
import csv
if __name__ == '__main__':
csvRaw = csv.dictreader(open("propertyOutput.csv",delimiter=','))
for row in csvRaw:
print row
我得到以下输出:
[ nan nan nan nan]
[ nan nan nan nan]
[ nan nan nan nan]
[ nan nan nan nan]...
有一堆数据,一些文字,一些应该吐出的数字。
来自csv文件的数据样本:
_id, zip, user_id, name
ObjectID(525aae), 11101, ObjectID(52389889), Property 1
ObjectID(525afec3), ca4020, ObjectID(52389d1bc5f59), San diego beach house
如何获取正确的数据?
答案 0 :(得分:2)
我看不出你的输出是如何来自该代码的。 Python不起作用,正如@JonClements指出的那样,表示显然不是来自你展示的print
。您应该看到的内容将是这样的:
>>> import csv
>>> with open("propertyOutput.csv", "rb") as fp:
... reader = csv.DictReader(fp, skipinitialspace=True)
... for row in reader:
... print row
...
{'_id': 'ObjectID(525aae)', 'user_id': 'ObjectID(52389889)', 'name': 'Property 1', 'zip': '11101'}
{'_id': 'ObjectID(525afec3)', 'user_id': 'ObjectID(52389d1bc5f59)', 'name': 'San diego beach house', 'zip': 'ca4020'}