Read long int from binary file

Enrico Morelli enrico_morelli at yahoo.com
Wed Oct 15 05:40:01 EDT 2003


On Wed, 15 Oct 2003 08:06:56 +0000, Alex Martelli wrote:

> Enrico Morelli wrote:
>    ...
>> I do know the format (in advance).
>> I have some binary files coming from SGI boxes and other from Linux boxes.
>> These files contains 32 bit long integer data that I need to read and
>> display in some graphic format.
>> In some cases I have to reverse the byte order in other not.
>> 
>> I'm unable to read these files and put the data in some array.
>> I tried to use f.read(4), but I have 4 numbers not one.
> 
> 
> import array
> 
> x1 = array('l')
> f1 = file('file_ok.dat', 'rb')
> x1.fromfile(f1)
> f1.close()
> 
> x2 = array('l')
> f2 = file('file_toswap.dat', 'rb')
> x2.fromfile(f2)
> x2.byteswap()
> f2.close()
> 
> 
> Alex

Thanks Alex!!!

A question, the fromfile syntax wants the n items to read.
x1.fromfile(f1,1) read exactly one 32 bit long integer?


Enrico

PS. Sei sempre 'r mejo!!




More information about the Python-list mailing list