finding byte order

Scott David Daniels Scott.Daniels at Acm.Org
Fri Dec 3 13:58:44 EST 2004


biner wrote:
 >   I am using a program that has to read binary data from files coming
 > from different machines. The file are always written with big endian.

Diez B. Roggisch wrote:
  [Scott David Daniels wrote]
>>How about sys.byteorder?
> This doesn't help, as he wants to read files from varying endianess - what
> the _current_ endianess is doesn't matter here.

But, in fact, he says the files are always big endian.  So, code like
the following should address his problem.  Note I use type 'h' as an
example so I can easily read samples.

     import sys, array
     f =open('huge.dat')
     v = array.array('h')   # Or whatever data type
     v.fromfile(f, 4096)
     f.close()
     if sys.byteorder == 'little':
         v.byteswap()

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list