Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ValueError: too many values to unpack

30 views
Skip to first unread message

fscked

unread,
Apr 11, 2007, 1:13:38 PM4/11/07
to
Trying to use CSV to read in a line with 11 fields and I keep getting
this error. I have googled a bit and have been unable to figure it out.

shan...@gmail.com

unread,
Apr 11, 2007, 1:20:13 PM4/11/07
to
That happens when you try something like this:

a,b,c = [1,2,3,4]

It means there are more items on the right side than the left. You
probably have < 11 variables on the left side.

Laszlo Nagy

unread,
Apr 11, 2007, 1:26:49 PM4/11/07
to fscked, pytho...@python.org
fscked írta:

> Trying to use CSV to read in a line with 11 fields and I keep getting
> this error. I have googled a bit and have been unable to figure it out.
>
>
Probably you have more than 11 values in some (or all) of the rows in
the CSV file. Try this code:

L = (1,2,3,4,5)
a1,a2,a3 = L

If you are sure that you only need a certain number of values, "the
first N columns":

a1,a2,a3 = L[:3]


Then you still can have a "not enough values to unpack" error, guess
what that means. ;-)

Laszlo

fscked

unread,
Apr 11, 2007, 3:28:08 PM4/11/07
to

Hmm, well I have counted the fields in the CSV and verified there are
only 11. Here is the offending code:

myfile = open('ClientsXMLUpdate.csv')
csvreader = csv.reader(myfile)

for boxid, mac, activated, hw_ver, sw_ver, heartbeat, name, address,
phone, country, city in csvreader:
mainbox = SubElement(root, "{Boxes}box")
mainbox.attrib["city"] = city
mainbox.attrib["country"] = country
mainbox.attrib["phone"] = phone
mainbox.attrib["address"] = address
mainbox.attrib["name"] = name
mainbox.attrib["pl_heartbeat"] = heartbeat
mainbox.attrib["sw_ver"] = sw_ver
mainbox.attrib["hw_ver"] = hw_ver
mainbox.attrib["date_activated"] = activated
mainbox.attrib["mac_address"] = mac
mainbox.attrib["boxid"] = boxid

I just don't get it... :/

Gabriel Genellina

unread,
Apr 11, 2007, 3:57:11 PM4/11/07
to
En Wed, 11 Apr 2007 16:28:08 -0300, fscked <fscke...@gmail.com>
escribió:

>> Trying to use CSV to read in a line with 11 fields and I keep getting
>> this error. I have googled a bit and have been unable to figure it
>

> myfile = open('ClientsXMLUpdate.csv')
> csvreader = csv.reader(myfile)
>
> for boxid, mac, activated, hw_ver, sw_ver, heartbeat, name, address,
> phone, country, city in csvreader:
> mainbox = SubElement(root, "{Boxes}box")

> [...]

You say all rows have 11 fields, the csv module insists on an error... try
using some print statements to see who is right:

for index, items in enumerate(csvreader):
print index, len(items)
if len(items)!=11: print items


--
Gabriel Genellina

Laszlo Nagy

unread,
Apr 11, 2007, 3:57:46 PM4/11/07
to fscked, pytho...@python.org

> Hmm, well I have counted the fields in the CSV and verified there are
> only 11. Here is the offending code:
>
>
....


Try this instead:

lineno = 0
for values in csvreader:
try:
lineno += 1


boxid, mac, activated, hw_ver, sw_ver, heartbeat, name,

address,phone, country, city = values
except:
print "Problem at line #",lineno
print repr(values)
break

Or even:

lineno = 0
for values in csvreader:
lineno += 1
if len(values) != 11:
print "Problem at line #",lineno
print repr(values)

Best,

Laszlo

John Machin

unread,
Apr 11, 2007, 4:14:38 PM4/11/07
to
On Apr 12, 5:28 am, "fscked" <fsckedag...@gmail.com> wrote:
> On Apr 11, 10:26 am, Laszlo Nagy <gand...@designaproduct.biz> wrote:
>
>
>
> > fscked írta:> Trying to use CSV to read in a line with 11 fields and I keep getting
> > > this error. I have googled a bit and have been unable to figure it out.
>
> > Probably you have more than 11 values in some (or all) of the rows in
> > the CSV file. Try this code:
>
> > L = (1,2,3,4,5)
> > a1,a2,a3 = L
>
> > If you are sure that you only need a certain number of values, "the
> > first N columns":
>
> > a1,a2,a3 = L[:3]
>
> > Then you still can have a "not enough values to unpack" error, guess
> > what that means. ;-)
>
> > Laszlo
>
> Hmm, well I have counted the fields in the CSV and verified there are
> only 11.

Counted how? Checked each line in the file? Let Python do it; see
below.

> Here is the offending code:
>
> myfile = open('ClientsXMLUpdate.csv')

Put in a second arg of 'rb'; if not the case now, someone might run
your code on Windows some day.

What platform, what version of Python?

> csvreader = csv.reader(myfile)
>
> for boxid, mac, activated, hw_ver, sw_ver, heartbeat, name, address,
> phone, country, city in csvreader:

Not exactly bullet-proof code.

> I just don't get it... :/

Possibly (in one or more rows) the address field has a comma in it and
it's not quoted properly.

Try writing your code in a more defensive fashion:
ENCOLS = 11
rownum = 0
for row in csvreader:
rownum += 1
ancols = len(row)
if ancols != ENCOLS:
print "Row %d has %d columns (expected %d)" \
% (rownum, ancols, ENCOLS)
print row
# pass/return/continue/break/raise/call error logger .....
(boxid, mac, activated, hw_ver,
sw_ver, heartbeat, name, address,
phone, country, city) = row

HTH,
John

fscked

unread,
Apr 11, 2007, 4:24:31 PM4/11/07
to
You guys have given me some great ideas, I am going to try them all
out and let you guys know how it turns out.

On a side note, thanks for nto bashing a noob like me who isn't the
greatest pythonista around. I am trying to learn and you guys are how
I learn my mistakes. Well you, and the fact the stuff occasionally
doesn't work. :)

Thanks again and I will report back in a couple hours (meetings).

Bruno Desthuilliers

unread,
Apr 11, 2007, 6:19:54 PM4/11/07
to
Laszlo Nagy a écrit :
(snip)

> Try this instead:
>
> lineno = 0
> for values in csvreader:
> try:
> lineno += 1

Laszlo, may I suggest using enumerate() here instead ?-)

for lineno, row in enumerate(csvreader):
print "line %s" % lineno+1 # want 1-based numbering

0 new messages