我看不到“要解压的值太多”异常如何适用于我的问题,如果确实如此,请解释
追溯:
c:\***>python graphJSON.py
Traceback (most recent call last):
File "graphJSON.py", line 17, in <module>
for region, four, one, two, three, threep in rows:
ValueError: too many values to unpack
我在这段简单的代码中遇到了太多的值错误,无法弄清楚问题是什么:错误来自 for 循环 which。我收到一条消息,说之前有人问过这个问题,但答案完全不清楚!
rows = csv.reader(open("graph.csv", "rb"))
# Init the the lists that will store our data
regions = []
fourHrs = []
oneDay = []
twoDay = []
threeDay = []
plusThreeDay = []
# Iterate through all the rows in our CSV
for region, four, one, two, three, threep in rows:
regions = regions + [region]
fourHrs = fourHrs + [four]
oneDay = oneDay + [one]
twoDay = twoDay + [two]
threeDay = threeDay + [three]
plusThreeDay = plusThreeDay + [threep]
# Format the output
output = {"data":[{"Regions":regions},
{"Four Hours":fourHrs},
{"One Day":oneDay},
{"Two Days":twoDay},
{"Three Days":threeDay},
{"More than Three Days":plusThreeDay}
]}
生成 JSON 文件 json_file = open("graph.json", "w") json.dump(output, json_file) csv中的数据如下:
First 28 25 10 2 7
Second 51 17 8 5 15
Third 38 33 24 7 19
回答:事实证明问题出在 CSV 上,它在一个阶段有更多列,我删除了这些列,但是,我认为在 excel 中引用并没有被完全删除。因此,在从 stratch 重做 CSV 时,它起作用了!