1

I do not see how "Too many values to unpack" Exception applies to my question, if it does please explain

Traceback:

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

I'm experiencing the too many values error with this simple peice of code and can't figure out what the issue is: The error comes from the for loop which. I've gotten a message saying this has been asked before however, the answer is totally unclear!

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}
    ]}

Generate the JSON file json_file = open("graph.json", "w") json.dump(output, json_file) The data in the csv look like:

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

Answered: It turns out the issue was with the CSV, it had more columns at one stage which I deleted, however, I think in excel the reference doesn't get deleted entirely. So, on redoing the CSV from stratch it worked!

Community
  • 1
  • 1
John Henry
  • 165
  • 1
  • 4
  • 14
  • 2
    You should paste the exact traceback. – djc May 29 '13 at 12:56
  • 1
    `rows` is an iterator of the entire file. You're trying to unpack all of the rows, each into 1 variable. You have too many rows to do that, and it's not what you want to do anyway. – Wooble May 29 '13 at 12:57
  • Wooble, that can't be the issue, as I've unpacked far more than this before – John Henry May 29 '13 at 12:58
  • You don't want to unpack `rows`, you want to unpack a single row. – Wooble May 29 '13 at 12:59
  • @Wooble: Are you serious? It's not a duplicate since the question wasn't answered – aldeb May 29 '13 at 13:00
  • @segfolt: it's a duplicate in that it's the exact same question asked by the exact same person. – Wooble May 29 '13 at 13:01
  • @Wooble: I don't think so because the question is much more specific this time. A lot of new elements have been added – aldeb May 29 '13 at 13:02
  • The old question has been edited so that it has this information and it's got reopen votes, which is how things are supposed to work. But now that this copy has answers, I guess closing it and reopening the other is pointless. – Wooble May 29 '13 at 13:09
  • @Wobble: one, original question wasn't asnwered, two, your suggests are way off as I just realized it was an issue with the csv and it's now working fine, WITH ROWS – John Henry May 29 '13 at 13:09
  • See, and that was the problem. Each row had more than six elements/columns. "too many elements to unpack" means that you are trying to assign too many elements to too few variables. In your case you had six variables (`region`, `four`, `one`, `three`, `threep`), but each row had more than six values. Python didn't know what to do with the remaining one. The duplicate question (http://stackoverflow.com/q/1479776/218196) explains exactly that: *"That exception means that you are trying to unpack a tuple, but the tuple has too many values with respect to the number of target variables."* – Felix Kling May 29 '13 at 14:26

2 Answers2

3

First of all, as @Wooble has pointed out, you have to loop over each of the lines in the csv file, and not through the csv file itself.

Once that is done, the exception would be caused by the line:

for region, four, one, two, three, threep in rows:

which you can confirm via the exception traceback.

The problem is caused because rows has less or more items than the target items in which your code is setting it to expand:

region, four, one, two, three, threep

i.e. less/more than 6 items.

Joseph Victor Zammit
  • 14,760
  • 10
  • 76
  • 102
0

It likely happens because you have one or more rows in your CSV file that has fewer than 6 fields. Tuple unpacking is used to unpack the row returned from the CSV file iterator to the six variable names you list in the header of the for-loop; the error makes a lot of sense to me!

djc
  • 11,603
  • 5
  • 41
  • 54