How to Read Bytes from a file

John Machin sjmachin at lexicon.net
Thu Mar 1 17:28:32 CET 2007


On Mar 2, 12:53 am, "Bart Ogryczak" <B.Ogryc... at gmail.com> wrote:
>
> import struct
> buf = open('somefile','rb').read()
> count1 = lambda x: (x&1)+(x&2>0)+(x&4>0)+(x&8>0)+(x&16>0)+(x&32>0)+
> (x&64>0)+(x&128>0)
> byteOnes = map(count1,struct.unpack('B'*len(buf),buf))

byteOnes = map(count1,struct.unpack('%dB'%len(buf),buf))




More information about the Python-list mailing list