473,651 Members | 2,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 68447
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...@gm ail.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...@design aproduct.bizwro te:
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('ClientsXM LUpdate.csv')
csvreader = csv.reader(myfi le)

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_heartbe at"] = heartbeat
mainbox.attrib["sw_ver"] = sw_ver
mainbox.attrib["hw_ver"] = hw_ver
mainbox.attrib["date_activated "] = activated
mainbox.attrib["mac_addres s"] = 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*********@gm ail.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('ClientsXM LUpdate.csv')
csvreader = csv.reader(myfi le)

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(csvre ader):
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...@gm ail.comwrote:
On Apr 11, 10:26 am, Laszlo Nagy <gand...@design aproduct.bizwro te:
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('ClientsXM LUpdate.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(myfi le)

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(csvre ader):
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
4870
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 scripting language, Python. I have tried everything by I'm going crazy here. Here is what I have...
8
3622
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 with a,b, and c Now I would like to add a special fourth config value to "T": ("T",1,5,4,0.005),
5
13818
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: '\x00\x00\xb9\x02\x13EXCLUDE_CREDIT_CARD' # the above was printed using repr(xbuffer). # Note that int(0x13) = 19 which is exactly the length of the visible text #
3
24798
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 Stanford Mathematics Physics Berkeley Physics U.C.L.A Biology Genetics
2
1744
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, Compare results in tblItem ________________________________________________________________________________ 71 records found in pctrsqlstage case9125 71 records found in qa-sql2\pctr case9126
0
6848
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 this ..what might be the solution for this. If I use pyRXP iam not getting the mentioned error. Error information for the reference :
2
3749
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 reason I convert it to a float instead of just leaving it as a string in the loop is because I have to have it printed out as a float besides the names and then the average displayed underneath thx #read in data line by line
8
7114
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 unpack" I've googled it and it says that happens when you have too few or too many strings that don't match with the variables in number your trying to assign them too. Below are the lines in reading in:
2
4385
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 = struct.unpack('h', testValue)
0
1604
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', self.__read(src, 2))) header = int(struct.unpack('1i', self.__read(src, 4)))
0
8361
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.