[Tutor] [tutor] how to cast to stucture
Alan Gauld
alan.gauld at freenet.co.uk
Tue Sep 5 19:14:03 CEST 2006
> i have a complex data in binary file and i want to read its
> fields... the C way is to read file in buffer and then cast
> it to proper structure. Is there a way to do the same in
> Python or i have to read the data byte by byte ?
You can use the struct module to unpack an arbitrary
sequence of bytes. You can find a very basic intro to
struct in the File Handliung topic of my tutorial.
But its not as simple as in C where you can more
or less map a memory area onto a structuure, you'll
need to extract each field and inert it into the
equivalent Python data structure - but if that is a
class you can write a method (load say?) to do the
heavy work and just call load(file) or whatever when
needed.
Alan G.
More information about the Tutor
mailing list