[Tutor] Bits

Kent Johnson kent37 at tds.net
Tue Apr 4 13:51:46 CEST 2006


Øyvind wrote:
> Hello.
> 
> Is it possible to read the bits (the 0's and 1's) of a string or a file
> with Python? What module would I use?

I don't know exactly what you mean by "read the bits" but you can use
data = open('afile', 'b').read() to get the data into a string
byte1=ord(data[1]) to get a character as binary data
byte1 & 1 to extract a single bit

You might also be interested in the struct module which lets you extract 
larger data (e.g. ints and floats) from a string.

Kent



More information about the Tutor mailing list