Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problems when unpacking tuple ...

Reply
Thread Tools

problems when unpacking tuple ...

 
 
harold
Guest
Posts: n/a
 
      04-22-2006
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 -

 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      04-22-2006
> 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




 
Reply With Quote
 
 
 
 
Rene Pijlman
Guest
Posts: n/a
 
      04-22-2006
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
 
Reply With Quote
 
Gerard Flanagan
Guest
Posts: n/a
 
      04-22-2006

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

 
Reply With Quote
 
harold
Guest
Posts: n/a
 
      04-22-2006
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 -

 
Reply With Quote
 
harold
Guest
Posts: n/a
 
      04-22-2006
Thank you Gerard.
This must be the problem. Now I can get it working.

 
Reply With Quote
 
Rene Pijlman
Guest
Posts: n/a
 
      04-22-2006
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
 
Reply With Quote
 
Felipe Almeida Lessa
Guest
Posts: n/a
 
      04-22-2006
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.

 
Reply With Quote
 
Gerard Flanagan
Guest
Posts: n/a
 
      04-22-2006

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

 
Reply With Quote
 
Felipe Almeida Lessa
Guest
Posts: n/a
 
      04-22-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ternary operator and tuple unpacking -- What am I missing ? imageguy Python 7 01-13-2009 09:58 PM
Tuple parameter unpacking in 3.x Martin Geisler Python 20 10-11-2008 06:34 PM
Can my own objects support tuple unpacking? Patrick Toomey Python 2 03-28-2008 11:59 PM
Tuple Unpacking in raise James Stroud Python 3 06-21-2005 01:46 AM
Idiom for default values when unpacking a tuple Paul McGuire Python 8 11-24-2004 03:54 PM



Advertisments