473,394 Members | 1,658 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,394 software developers and data experts.

problems when unpacking tuple ...

Dear all,

Maybe I stared on the monitor for too long, because I cannot find the
bug ...
My script "transition_filter.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_filter.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 1601
> 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_filter.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_filter.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
Thanks for all your answer!
Of course, I wanted to assign the outcome of the split(), not to
iterate
over them. Thinks are done so easy in python that, sometimes, one
does not even notice that one actually does them ;-)
Cheers,

- harold -

Apr 22 '06 #11
On 23/04/2006 2:21 AM, harold wrote:
Dear all,

Maybe I stared on the monitor for too long, because I cannot find the
bug ...
You already have your answer, but below are clues on how to solve such
problems much faster by yourself.
My script "transition_filter.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']
I doubt it. Much more likely is
['0.0', '1', '0.04', '0']
It doesn't matter in this case, but you should really get into the
habit of copy/pasting *EXACTLY* what is there, not re-typing what you
think is there.
Traceback (most recent call last):
File "transition_filter.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??


Clues:
1. Use the print statement to show what you have.
2. Use the built-in repr() function to show *unambiguously* what you
have -- very important when you get into Unicode and encoding/decoding
problems; what you see after "print foo" is not necessarily what
somebody using a different locale/codepage will see.
3. In some cases (not this one), it is also helpful to print the type()
of the data item.

Example:

C:\junk>type harold.py
import sys

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

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

def harold2():
for line in sys.stdin:
print "line =", repr(line)
split_result = line.split()
print "split result =", repr(split_result)
for x in split_result:
print "about to try to unpack the sequence", repr(x), "into
4 items"

a, b, c, d = x

harold2()

C:\junk>harold.py
0.0 1 0.04 0
a b c d e f
^Z
line = '0.0 1 0.04 0\n'
split result = ['0.0', '1', '0.04', '0']
about to try to unpack the sequence '0.0' into 4 items
Traceback (most recent call last):
File "C:\junk\harold.py", line 22, in ?
harold2()
File "C:\junk\harold.py", line 20, in harold2
a, b, c, d = x
ValueError: need more than 3 values to unpack

=====

Coding style: Not inventing and using your own dialect makes two-way
communication much easier in any language (computer or human). Consider
reading and following http://www.python.org/dev/peps/pep-0008/

Hope this helps,
John
Apr 22 '06 #12

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

Similar topics

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...
1
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...
3
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...
3
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...
9
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...
16
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',...
13
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
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...
21
by: Martin Geisler | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkjlQNwACgkQ6nfwy35F3Tj8ywCgox+XdmeDTAKdN9Q8KZAvfNe4 0/4AmwZGClr8zmonPAFnFsAOtHn4JhfY =hTwE -----END PGP...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...

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.