我在MacBook Air上运行python。我有一个简单的例子,我试图在csv中使用prettytable for python读取:
from prettytable import from_csv`
fp = open("test.csv", "r")`
pt = from_csv(fp)`
fp.close()
下面是我的csv“test.csv”:
name,city,areacode
joe,chicago,312
sam,san diego,619
当我运行代码时,我收到以下错误:
%> python3 prettytable-testing2.py
Traceback (most recent call last):
File "prettytable-testing2.py", line 3, in <module>
pt = from_csv(fp)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/prettytable.py", line 1351, in from_csv
table.add_row([x.strip() for x in row])
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/prettytable.py", line 818, in add_row
raise Exception("Row has incorrect number of values, (actual) %d!=%d (expected)" %(len(row),len(self._field_names)))
Exception: Row has incorrect number of values, (actual) 0!=3 (expected)
好奇为什么当有明显的3个字段和2行带有数据的项目时它会获得0值。
由于
答案 0 :(得分:0)
我尝试了你的代码,但我没有得到同样的错误。
我认为你可能在文件的末尾或某个地方有一个空行。当我添加一个空行时,我得到了像你这样的错误:
Exception: Row has incorrect number of values, (actual) 0!=3 (expected)