473,549 Members | 3,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems when unpacking tuple ...

Dear all,

Maybe I stared on the monitor for too long, because I cannot find the
bug ...
My script "transition_fil ter.py" starts with the following lines:

import sys

for line in sys.stdin :
try :
for a,b,c,d in line.split() :
pass

except ValueError , err :
print line.split()
raise err

The output (when given the data I want to parse) is:
['0.0','1','0.04 ','0']
Traceback (most recent call last):
File "transition_fil ter.py", line 10, in ?
raise err
ValueError: need more than 3 values to unpack

What is going wrong here? Why does python think that
I want to unpack the outcome of line.split() into three
values instead of four? I must be really tired, but I just
cannot see the problem. Any clues??

Thanks,

- harold -

Apr 22 '06 #1
11 1616
> for a,b,c,d in line.split() :
[snip]
The output (when given the data I want to parse) is:
['0.0','1','0.04 ','0']


You'll notice that you're not passing any parameters to
split(). By default, it splits on whitespace, and your
input doesn't have any whitespace in it. Thus, you're
actually only getting *one* (not three) elements back. Try
using split(",") instead.

-tkc


Apr 22 '06 #2
harold:
The output (when given the data I want to parse) is:
If you'd told us that data, and told us what version of Python you're
using, we could have reproduced the problem to look into it.
ValueError: need more than 3 values to unpack

Why does python think that I want to unpack the outcome of
line.split() into three values instead of four?


That's not what it says. It says there are only 3 values in the outcome,
and it needs more (4 to be precise).

--
René Pijlman
Apr 22 '06 #3

harold wrote:
Dear all,

Maybe I stared on the monitor for too long, because I cannot find the
bug ...
My script "transition_fil ter.py" starts with the following lines:

import sys

for line in sys.stdin :
try :
for a,b,c,d in line.split() :
pass

except ValueError , err :
print line.split()
raise err

The output (when given the data I want to parse) is:
['0.0','1','0.04 ','0']
Traceback (most recent call last):
File "transition_fil ter.py", line 10, in ?
raise err
ValueError: need more than 3 values to unpack

What is going wrong here? Why does python think that
I want to unpack the outcome of line.split() into three
values instead of four? I must be really tired, but I just
cannot see the problem. Any clues??


The 3 values are coming from the first element in the list '0.0' -
change it to '0' and you should get: ValueError: need more than 1 value
to unpack.
See? it's trying to get a,b,c,d from each element of the list not the
whole list.

Gerard

Apr 22 '06 #4
Rene Pijlman schrieb:
harold:
The output (when given the data I want to parse) is:


If you'd told us that data, and told us what version of Python you're
using, we could have reproduced the problem to look into it.


Thank you for the answers and sorry that I did not provide more
information in the first place.
My data file is white space seperated data (space seperated data to be
precise) and I am
using python 2.4.2
As can be seen, the output of the print statement in the lines

except ValueError , err:
print line.split()
raise err

has exactly four values...

ValueError: need more than 3 values to unpack

Why does python think that I want to unpack the outcome of
line.split() into three values instead of four?


That's not what it says. It says there are only 3 values in the outcome,
and it needs more (4 to be precise).

A similar error happens in an interpreter session, when typing
for line in ["1 2 3 4"] :

.... for a,b,c,d in line.split() :
.... pass
....
Traceback (most recent call last):
File "<stdin>", line 2, in ?
ValueError: need more than 1 value tyo unpack

maybe this might help to track down the error.
Thanks!

- harold -

Apr 22 '06 #5
Thank you Gerard.
This must be the problem. Now I can get it working.

Apr 22 '06 #6
Em Sáb, 2006-04-22 Ã*s 09:21 -0700, harold escreveu:
for line in sys.stdin :
try :
for a,b,c,d in line.split() :
pass

except ValueError , err :
print line.split()
raise err


Try this:

for a, b, c, d in sys.stdin:
print a, b, c, d

--
Felipe.

Apr 22 '06 #7
harold:
A similar error happens in an interpreter session, when typing
for line in ["1 2 3 4"] :

... for a,b,c,d in line.split() :
... pass
...
Traceback (most recent call last):
File "<stdin>", line 2, in ?
ValueError: need more than 1 value tyo unpack

maybe this might help to track down the error.


Suppose the code was:

for x in line.split():

line.split() yields one list with 4 items. The loop will be performed 4
times, assigning one value of the list to x with every iteration. In your
code, x is a tuple with 4 elements: a,b,c,d. So with every iteration one
value is assigned to that tuple. Since the value is not a sequence of 4
items, this fails.

There's two sensible things you can do:

for line in ["1 2 3 4"]:
a,b,c,d = line.split()

for line in ["1 2 3 4"]:
for a in line.split():

--
René Pijlman
Apr 22 '06 #8
Em Sáb, 2006-04-22 Ã*s 14:25 -0300, Felipe Almeida Lessa escreveu:
Em Sáb, 2006-04-22 Ã*s 09:21 -0700, harold escreveu:
for line in sys.stdin :
try :
for a,b,c,d in line.split() :
pass

except ValueError , err :
print line.split()
raise err


Try this:

for a, b, c, d in sys.stdin:
print a, b, c, d


Forget that. It was stupid. You should try this instead:

for line in sys.stdin:
a, b, c, d = line.split()

--
Felipe.

Apr 22 '06 #9

harold wrote:
Thank you Gerard.
This must be the problem. Now I can get it working.


Good! I got confused thinking about it too, but I think you just had
one loop too many.

for line in sys.stdin :
try :
a,b,c,d = line.split()

not:

for line in sys.stdin :
try :
for a,b,c,d in line.split() :
pass

Gerard

Apr 22 '06 #10

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

Similar topics

8
3617
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),
1
2906
by: DJTB | last post by:
zodb-dev@zope.org] Hi, I'm having problems storing large amounts of objects in a ZODB. After committing changes to the database, elements are not cleared from memory. Since the number of objects I'd like to store in the ZODB is too large to fit in RAM, my program gets killed with signal 11 or signal 9... Below a minimal working (or...
3
1826
by: James Stroud | last post by:
Hello All, Is this a bug? Why is this tuple getting unpacked by raise? Am I missing some subtle logic? Why does print not work the same way as raise? Both are statements. Why does raise need to be so special? py> sometup = 1,2 py> print sometup (1, 2) py> print 1,2,3, sometup
3
2742
by: localpricemaps | last post by:
i am having a problem writing a tuple to a text file. my code is below. what i end up getting is a text file that looks like this burger, 7up burger, 7up burger, 7up and this is instead of getting a list that should look like this
9
1409
by: tkpmep | last post by:
I have a list y >>>y from which I want to extract only the 2nd and 4th item by partially unpacking the list. So I tried >>>a,b = y Traceback (most recent call last): File "<interactive input>", line 1, in ? TypeError: list indices must be integers
16
1324
by: John Salerno | last post by:
I'm a little confused, but I'm sure this is something trivial. I'm confused about why this works: ('more', 'less'), ('something', 'nothing'), ('good', 'bad')) (('hello', 'goodbye'), ('more', 'less'), ('something', 'nothing'), ('good', 'bad')) print x
13
2943
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
4
4607
by: McA | last post by:
Hi all, probably a dumb question, but I didn't find something elegant for my problem so far. In perl you can unpack the element of a list to variables similar as in python (a, b, c = ), but the number of variables need not to fit the number of list elements. That means, if you have less list elements variables are filled with
21
7233
by: Martin Geisler | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkjlQNwACgkQ6nfwy35F3Tj8ywCgox+XdmeDTAKdN9Q8KZAvfNe4 0/4AmwZGClr8zmonPAFnFsAOtHn4JhfY =hTwE -----END PGP SIGNATURE-----
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
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...
0
7809
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...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1936
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
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
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...

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.