python错误:值太多,无法取消固定

2024-04-25 08:48:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我不明白"Too many values to unpack" Exception如何适用于我的问题,如果是,请解释

回溯:

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循环。我收到一条消息说这是以前问过的,但是答案完全不清楚!

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”) dump(输出,json_文件) csv中的数据看起来像:

First   28  25  10  2   7 
Second  51  17  8   5   15 
Third   38  33  24  7   19

答:原来是CSV的问题,它在一个阶段有更多的列被我删除了,但是,我认为在excel中引用并没有被完全删除。所以,从stratch重做CSV时它起作用了!


Tags: theinforplusoneregionrowsfour