Read Binary data

mblume mblume at socha.net
Thu Sep 4 19:03:33 CEST 2008


Am Thu, 04 Sep 2008 18:03:54 +0200 schrieb Fredrik Lundh:
>
>>   I am trying to read a binary file [...]
> 
> 
> f = open("a.bin", "rb") # read binary data 
> s = f.read() # read all bytes into a string
> 
> import array, sys
> 
> a = array.array("f", s) # "f" for float 
> if sys.byteorder != "big":
>     a.byteswap()
> 
For more complicated structures, the struct module may help.

HTH.
Martin



More information about the Python-list mailing list