problems when unpacking tuple ...

Gerard Flanagan grflanagan at yahoo.co.uk
Sat Apr 22 12:41:42 EDT 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




More information about the Python-list mailing list