How to Read Bytes from a file
Alex Martelli
aleax at mac.com
Thu Mar 1 02:00:26 EST 2007
gregpinero at gmail.com <gregpinero at gmail.com> wrote:
> It seems like this would be easy but I'm drawing a blank.
>
> What I want to do is be able to open any file in binary mode, and read
> in one byte (8 bits) at a time and then count the number of 1 bits in
> that byte.
>
> I got as far as this but it is giving me strings and I'm not sure how
> to accurately get to the byte/bit level.
>
> f1=file('somefile','rb')
> while 1:
> abyte=f1.read(1)
You should probaby prepare before the loop a mapping from char to number
of 1 bits in that char:
m = {}
for c in range(256):
m[c] = countones(c)
and then sum up the values of m[abyte] into a running total (break from
the loop when 'not abyte', i.e. you're reading 0 bytes even though
asking for 1 -- that tells you the fine is finished, remember to close
it).
A trivial way to do the countones function:
def countones(x):
assert x>=0
c = 0
while x:
c += (x&1)
x >>= 1
return c
you just don't want to call it too often, whence the previous advice to
call it just 256 times to prep a mapping.
If you download and install gmpy you can use gmpy.popcount as a fast
implementation of countones:-).
Alex
More information about the Python-list
mailing list