problems when unpacking tuple ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • harold

    #1

    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 -

  • Tim Chase

    #2
    Re: problems when unpacking tuple ...

    > for a,b,c,d in line.split() :[color=blue]
    >[/color]
    [snip][color=blue]
    >
    > The output (when given the data I want to parse) is:
    > ['0.0','1','0.04 ','0'][/color]

    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




    Comment

    • Rene Pijlman

      #3
      Re: problems when unpacking tuple ...

      harold:[color=blue]
      >The output (when given the data I want to parse) is:[/color]

      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.
      [color=blue]
      >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?[/color]

      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

      Comment

      • Gerard Flanagan

        #4
        Re: problems when unpacking tuple ...


        harold wrote:[color=blue]
        > 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??
        >[/color]

        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

        Comment

        • harold

          #5
          Re: problems when unpacking tuple ...

          Rene Pijlman schrieb:
          [color=blue]
          > harold:[color=green]
          > >The output (when given the data I want to parse) is:[/color]
          >
          > 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.
          >[/color]

          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...

          [color=blue][color=green]
          > >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?[/color]
          >
          > That's not what it says. It says there are only 3 values in the outcome,
          > and it needs more (4 to be precise).[/color]


          A similar error happens in an interpreter session, when typing[color=blue][color=green][color=darkred]
          >>> for line in ["1 2 3 4"] :[/color][/color][/color]
          .... 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 -

          Comment

          • harold

            #6
            Re: problems when unpacking tuple ...

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

            Comment

            • Felipe Almeida Lessa

              #7
              Re: problems when unpacking tuple ...

              Em Sáb, 2006-04-22 às 09:21 -0700, harold escreveu:[color=blue]
              > for line in sys.stdin :
              > try :
              > for a,b,c,d in line.split() :
              > pass
              >
              > except ValueError , err :
              > print line.split()
              > raise err[/color]

              Try this:

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

              --
              Felipe.

              Comment

              • Rene Pijlman

                #8
                Re: problems when unpacking tuple ...

                harold:[color=blue]
                >A similar error happens in an interpreter session, when typing[color=green][color=darkred]
                >>>> for line in ["1 2 3 4"] :[/color][/color]
                >... 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.[/color]

                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

                Comment

                • Felipe Almeida Lessa

                  #9
                  Re: problems when unpacking tuple ...

                  Em Sáb, 2006-04-22 às 14:25 -0300, Felipe Almeida Lessa escreveu:[color=blue]
                  > Em Sáb, 2006-04-22 às 09:21 -0700, harold escreveu:[color=green]
                  > > for line in sys.stdin :
                  > > try :
                  > > for a,b,c,d in line.split() :
                  > > pass
                  > >
                  > > except ValueError , err :
                  > > print line.split()
                  > > raise err[/color]
                  >
                  > Try this:
                  >
                  > for a, b, c, d in sys.stdin:
                  > print a, b, c, d
                  >[/color]

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

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

                  --
                  Felipe.

                  Comment

                  • Gerard Flanagan

                    #10
                    Re: problems when unpacking tuple ...


                    harold wrote:[color=blue]
                    > Thank you Gerard.
                    > This must be the problem. Now I can get it working.[/color]

                    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

                    Comment

                    • harold

                      #11
                      Re: problems when unpacking tuple ...

                      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 -

                      Comment

                      • John Machin

                        #12
                        Re: problems when unpacking tuple ...

                        On 23/04/2006 2:21 AM, harold wrote:[color=blue]
                        > Dear all,
                        >
                        > Maybe I stared on the monitor for too long, because I cannot find the
                        > bug ...[/color]

                        You already have your answer, but below are clues on how to solve such
                        problems much faster by yourself.
                        [color=blue]
                        > 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'][/color]

                        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.
                        [color=blue]
                        > 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??
                        >[/color]

                        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_resu lt)
                        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

                        Comment

                        Working...