using struct module on a file

U

Ulrich Eckhardt

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
 
J

Jon Clements

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

Simon Forman

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top