473,326 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

ValueError: too many values to unpack

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.

Apr 11 '07 #1
8 68409
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.

On Apr 11, 10:13 am, "fscked" <fsckedag...@gmail.comwrote:
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.

Apr 11 '07 #2
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

Apr 11 '07 #3
On Apr 11, 10:26 am, Laszlo Nagy <gand...@designaproduct.bizwrote:
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. 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... :/

Apr 11 '07 #4
En Wed, 11 Apr 2007 16:28:08 -0300, fscked <fs*********@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
Apr 11 '07 #5
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

Apr 11 '07 #6
On Apr 12, 5:28 am, "fscked" <fsckedag...@gmail.comwrote:
On Apr 11, 10:26 am, Laszlo Nagy <gand...@designaproduct.bizwrote:
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

Apr 11 '07 #7
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).

Apr 11 '07 #8
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
Apr 11 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Raoul | last post by:
I wrote a COM server object in C++ a few months ago. I can use it from Visual Basic, Visual C++, S-Plus and a number of other scripting environments. What I can't do is use it with my FAVOURITE...
8
by: Paul McGuire | last post by:
I'm trying to manage some configuration data in a list of tuples, and I unpack the values with something like this: configList = for data in configList: name,a,b,c = data ... do something...
5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
3
by: Elezar Simeon Papo | last post by:
Hello All, I have a tab separated input file (data.txt) in text format - the file looks like this SCHOOL DEPART1 DEPART2 DEPART3 Harvard Economics Mathematics Physics...
2
by: k.retheesh | last post by:
Hi, I am very new to pyton, during my adventures journey I got the following error message which am not able to solve, can somebody help me. I was trying to format my output in a readable way, ...
0
by: laxmikiran.bachu | last post by:
ValueError: need more than 1 value to unpack . This is the error I get when I am using pyRXPU in the one of the scripts to get the strings from an application. Can anybody throw some light on...
2
by: Shawn Minisall | last post by:
I'm trying to unpack a list of 5 floats from a list read from a file and python is telling me 5 variables are too many for the string.split statement. Anyone have any other idea's? NOTE: the only...
8
by: Shawn Minisall | last post by:
I am trying to read a few lines of a file with multiple values, the rest are single and are reading in fine. With the multiple value lines, python says this "ValueError: too many values to...
2
by: brnstrmrs | last post by:
If I run: testValue = '\x02\x00' junk = struct.unpack('h', testValue) Everything works but If I run testValue = raw_input("Enter Binary Code..:") inputting at the console '\x02\x00' junk...
0
by: Ping Zhao | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am writing a small program to decode MS bitmap image. When I use statements as follow, it works fine: header = str(struct.unpack('2s',...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.