Newbie question: how to process a binary file

Paul Stillwell paul at polariscomm.com
Thu May 13 18:25:27 EDT 1999


Hi,

I am new to Python so I'm sorry if this question has been asked and
answered.  I have looked all over and can't find the info I need.  Here is
the problem:

I am writing an application that FTPs to a server and gets a binary file.
The file has a known format (to me) and I need to parse the file into bytes
(8 bits), longs (32 bits) and long longs (64 bits) based on the format of
the file.  So, there could be 4 bytes followed by a long long, folowed by
another 4 bytes, etc.  To further complicate the problem, some of the longs
will have to be byte swapped.  The reading of the bytes seems fairly
straight forward (using file.read(1)), but getting the data into the correct
type seems to be the trick.  If I were doing this in C I could just cast the
data, but I'm not sure how to achieve the same effect in Python.

So, any suggestions?  I am wide open to any ideas anyone may have.  I am
concerned about the time it will take to process the file (I actually have 3
different files to download and parse.  Each file having a different
format), so I don't want to apply my C mindset to this problem because I am
sure that I will end up with slower code and a much less elegant solution.

Thanks for any help you can provide!

Paul
paul at polariscomm.com







More information about the Python-list mailing list