struct.unpack

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

    struct.unpack

    Hello Everybody,

    I've read a pipe and store it in a object.
    My next step was the separation from 4 bytes with
    obj = string.join(lis t(dataObject)[:4] ==> '\x16 \x00 \x00 \x00'
    and the converting by
    value = struct.unpack(' I', obj) generated the error
    "unpack str size does not match format"

    Unfortunately is len(obj) 7, but integer lengt 4.
    Why 7 ?

    Any ideas ?

    gf
  • Fredrik Lundh

    #2
    Re: struct.unpack

    "g.franzkow iak" wrote:
    [color=blue]
    > I've read a pipe and store it in a object.
    > My next step was the separation from 4 bytes with
    > obj = string.join(lis t(dataObject)[:4] ==> '\x16 \x00 \x00 \x00'
    > and the converting by
    > value = struct.unpack(' I', obj) generated the error
    > "unpack str size does not match format"
    >
    > Unfortunately is len(obj) 7, but integer lengt 4.
    > Why 7 ?[/color]

    because string.join inserts a space between the bytes, by default (as
    your example shows)

    if you really need that join(list) thing (it's not clear how you read it, but
    I find it a bit hard to believe that you've managed to read it into some-
    thing that's supported by list but not unpack), you can do

    obj = string.join(lis t(dataObject)[:4], "")

    or

    obj = "".join(list(da taObject[:4]))

    or some variation thereof.

    </F>



    Comment

    • g.franzkowiak

      #3
      Re: struct.unpack

      Fredrik Lundh schrieb:[color=blue]
      > "g.franzkow iak" wrote:
      >
      >[color=green]
      >>I've read a pipe and store it in a object.
      >>My next step was the separation from 4 bytes with
      >>obj = string.join(lis t(dataObject)[:4] ==> '\x16 \x00 \x00 \x00'
      >>and the converting by
      >>value = struct.unpack(' I', obj) generated the error
      >>"unpack str size does not match format"
      >>
      >>Unfortunate ly is len(obj) 7, but integer lengt 4.
      >>Why 7 ?[/color]
      >
      >
      > because string.join inserts a space between the bytes, by default (as
      > your example shows)
      >
      > if you really need that join(list) thing (it's not clear how you read it, but
      > I find it a bit hard to believe that you've managed to read it into some-
      > thing that's supported by list but not unpack), you can do
      >
      > obj = string.join(lis t(dataObject)[:4], "")
      >
      > or
      >
      > obj = "".join(list(da taObject[:4]))
      >
      > or some variation thereof.
      >
      > </F>
      >
      >
      >[/color]

      I've also found the problem with som tests.
      The best of this was:
      tmpList = list(dataObject )[:4])
      obj = tmpList[0]+tmpList[1]+tmpList[2]+tmpList[3].

      But your suggestions are much better and new for me - both.

      Thanks
      gerd

      Comment

      • Peter Otten

        #4
        Re: struct.unpack

        g.franzkowiak wrote:
        [color=blue]
        > tmpList = list(dataObject )[:4])
        > obj = tmpList[0]+tmpList[1]+tmpList[2]+tmpList[3].[/color]

        Have you tried just

        obj = dataObject[:4]

        without the intermediate list? If that failed, can you tell us the type of
        the dataObject? E. g.
        [color=blue][color=green][color=darkred]
        >>> print type(dataObject )[/color][/color][/color]
        <class '__main__.Neith erListNorString '>

        Peter

        Comment

        • g.franzkowiak

          #5
          Re: struct.unpack

          Peter Otten schrieb:[color=blue]
          > g.franzkowiak wrote:
          >
          >[color=green]
          >>tmpList = list(dataObject )[:4])
          >>obj = tmpList[0]+tmpList[1]+tmpList[2]+tmpList[3].[/color]
          >
          >
          > Have you tried just
          >
          > obj = dataObject[:4]
          >
          > without the intermediate list? If that failed, can you tell us the type of
          > the dataObject? E. g.
          >
          >[color=green][color=darkred]
          >>>>print type(dataObject )[/color][/color]
          >
          > <class '__main__.Neith erListNorString '>
          >
          > Peter
          >[/color]

          The dataObject was read from a named pipe as an byte stream

          state, dataObject = win32file.ReadF ile(handle, nbytes, None)
          print repr(dataObject )
          ==> '\x01\x02\x03\x 04\x00\x00\x00\ x00\x00\x00\x0. ....

          With Frederiks help operates this fine

          gerd

          Comment

          • Peter Otten

            #6
            Re: struct.unpack

            g.franzkowiak wrote:
            [color=blue]
            > The dataObject was read from a named pipe as an byte stream
            >
            > state, dataObject = win32file.ReadF ile(handle, nbytes, None)
            > print repr(dataObject )
            > ==> '\x01\x02\x03\x 04\x00\x00\x00\ x00\x00\x00\x0. ....
            >
            > With Frederiks help operates this fine[/color]

            I do not doubt that. My point was that instead of the suggested

            # Fredrik
            obj = "".join(list(da taObject[:4]))

            the simpler

            # me
            obj = dataObject[:4]

            might work as well. I cannot test it here, but judging from



            the resulting dataObject is a buffer and buffer slices seem to be just
            strings.
            [color=blue]
            > print repr(dataObject )
            > ==> '\x01\x02\x03\x 04\x00\x00\x00\ x00\x00\x00\x0. ....[/color]

            Hmm, that looks as if dataObject were a string -- please post the result of

            print type(dataObject )

            just to help me restore my peace of mind :-)

            Peter

            Comment

            • g.franzkowiak

              #7
              Re: struct.unpack

              Peter Otten schrieb:[color=blue]
              > g.franzkowiak wrote:
              >
              >[color=green]
              >>The dataObject was read from a named pipe as an byte stream
              >>
              >>state, dataObject = win32file.ReadF ile(handle, nbytes, None)
              >>print repr(dataObject )
              >> ==> '\x01\x02\x03\x 04\x00\x00\x00\ x00\x00\x00\x0. ....
              >>
              >>With Frederiks help operates this fine[/color]
              >
              >
              > I do not doubt that. My point was that instead of the suggested
              >
              > # Fredrik
              > obj = "".join(list(da taObject[:4]))
              >
              > the simpler
              >
              > # me
              > obj = dataObject[:4]
              >
              > might work as well. I cannot test it here, but judging from
              >
              > http://aspn.activestate.com/ASPN/doc...File_meth.html
              >
              > the resulting dataObject is a buffer and buffer slices seem to be just
              > strings.
              >
              >[color=green]
              >>print repr(dataObject )
              >> ==> '\x01\x02\x03\x 04\x00\x00\x00\ x00\x00\x00\x0. ....[/color]
              >
              >
              > Hmm, that looks as if dataObject were a string -- please post the result of
              >
              > print type(dataObject )
              >
              > just to help me restore my peace of mind :-)
              >
              > Peter
              >[/color]

              Hello Peter,

              was a node in my mind, the data comes as a string (with readfile always)
              << type(dataObject ) = 'str' >> and
              << type(dataObject :4) = 'str' >> also ;-)

              I've dropped the loop with list/join and the result is the same.
              Thank you :-))

              gerd

              Comment

              Working...