using struct module on a file

Discussion in 'Python' started by Ulrich Eckhardt, Nov 18, 2009.

  1. Hia!

    I need to read a file containing packed "binary" data. For that, I find the
    struct module pretty convenient. What I always need to do is reading a chunk
    of data from the file (either using calcsize() or a struct.Struct instance)
    and then parsing it with unpack(). For that, I repeatedly write utility
    functions that help me do just that, but I can't imagine that there is no
    better way for that.

    Questions:
    0. Am I approaching this from the wrong direction? I'm not a programming
    noob, but rather new to Python still.
    1. The struct module has pack_into() or unpack_from(), could I somehow
    combine that with a file?
    2. Is there some easier way to read files? I know about array and xdrlib,
    but neither really fit my desires.
    3. Failing all that, would you consider this a useful addition to the struct
    module, i.e. should I write a feature request?

    Thanks!

    Uli
    Ulrich Eckhardt, Nov 18, 2009
    #1
    1. Advertising

  2. Ulrich Eckhardt

    Jon Clements Guest

    On Nov 18, 4:42 pm, Ulrich Eckhardt <> wrote:
    > Hia!
    >
    > I need to read a file containing packed "binary" data. For that, I find the
    > struct module pretty convenient. What I always need to do is reading a chunk
    > of data from the file (either using calcsize() or a struct.Struct instance)
    > and then parsing it with unpack(). For that, I repeatedly write utility
    > functions that help me do just that, but I can't imagine that there is no
    > better way for that.
    >
    > Questions:
    > 0. Am I approaching this from the wrong direction? I'm not a programming
    > noob, but rather new to Python still.
    > 1. The struct module has pack_into() or unpack_from(), could I somehow
    > combine that with a file?
    > 2. Is there some easier way to read files? I know about array and xdrlib,
    > but neither really fit my desires.
    > 3. Failing all that, would you consider this a useful addition to the struct
    > module, i.e. should I write a feature request?
    >
    > Thanks!
    >
    > Uli


    First time I've seen zero based indexing for paragraph markers :)

    unpack_from() will work on anything that supports the buffer
    interface.

    To work with files you can use something like:

    my4chars = struct.Struct('4c')
    def struct_read(s, f):
    return s.unpack_from(f.read(s.size))

    Which isn't hideously painful.

    Jon.
    Jon Clements, Nov 18, 2009
    #2
    1. Advertising

  3. Ulrich Eckhardt

    Simon Forman Guest

    On Wed, Nov 18, 2009 at 11:42 AM, Ulrich Eckhardt <> wrote:
    > Hia!
    >
    > I need to read a file containing packed "binary" data. For that, I find the
    > struct module pretty convenient. What I always need to do is reading a chunk
    > of data from the file (either using calcsize() or a struct.Struct instance)
    > and then parsing it with unpack(). For that, I repeatedly write utility
    > functions that help me do just that, but I can't imagine that there is no
    > better way for that.
    >
    > Questions:
    > 0. Am I approaching this from the wrong direction? I'm not a programming
    > noob, but rather new to Python still.
    > 1. The struct module has pack_into() or unpack_from(), could I somehow
    > combine that with a file?
    > 2. Is there some easier way to read files? I know about array and xdrlib,
    > but neither really fit my desires.
    > 3. Failing all that, would you consider this a useful addition to the struct
    > module, i.e. should I write a feature request?
    >
    > Thanks!
    >
    > Uli


    You might look at "Construct":

    http://construct.wikispaces.com/

    ~Simon
    Simon Forman, Nov 18, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    398
    RA Scheltema
    Jan 6, 2004
  2. Gunnar G

    struct in struct

    Gunnar G, May 31, 2004, in forum: C++
    Replies:
    14
    Views:
    798
  3. DanielEKFA
    Replies:
    8
    Views:
    606
    DanielEKFA
    May 16, 2005
  4. Chris Fogelklou
    Replies:
    36
    Views:
    1,373
    Chris Fogelklou
    Apr 20, 2004
  5. dutchgoldtony
    Replies:
    15
    Views:
    470
    Mike Wahler
    Nov 16, 2005
Loading...

Share This Page