Re: Reading file bit by bit

2010-06-08 Thread Martin
On Jun 7, 9:57 am, Alfred Bovin alf...@bovin.invalid wrote: Hi all. I'm working on something where I need to read a (binary) file bit by bit and do something depending on whether the bit is 0 or 1. Any help on doing the actual file reading is appreciated. Thanks in advance Hi, Have you

Reading file bit by bit

2010-06-07 Thread Alfred Bovin
Hi all. I'm working on something where I need to read a (binary) file bit by bit and do something depending on whether the bit is 0 or 1. Any help on doing the actual file reading is appreciated. Thanks in advance -- http://mail.python.org/mailman/listinfo/python-list

Re: Reading file bit by bit

2010-06-07 Thread Ulrich Eckhardt
Alfred Bovin wrote: I'm working on something where I need to read a (binary) file bit by bit and do something depending on whether the bit is 0 or 1. Well, smallest unit you can read is an octet/byte. You then check the individual digits of the byte using binary masks. f = open(...)

Re: Reading file bit by bit

2010-06-07 Thread Peter Otten
Alfred Bovin wrote: I'm working on something where I need to read a (binary) file bit by bit and do something depending on whether the bit is 0 or 1. Any help on doing the actual file reading is appreciated. The logical unit in which files are written is the byte. You can split the bytes

Re: Reading file bit by bit

2010-06-07 Thread Richard Thomas
On Jun 7, 10:17 am, Peter Otten __pete...@web.de wrote: Alfred Bovin wrote: I'm working on something where I need to read a (binary) file bit by bit and do something depending on whether the bit is 0 or 1. Any help on doing the actual file reading is appreciated. The logical unit in

Re: Reading file bit by bit

2010-06-07 Thread Ulrich Eckhardt
Ulrich Eckhardt wrote: data = f.read() for byte in data: for i in range(8): bit = 2**i byte ... Correction: Of course you have to use ord() to get from the single-element string (byte above) to its integral value first. Uli -- Sator Laser GmbH

Re: Reading file bit by bit

2010-06-07 Thread Lie Ryan
On 06/07/10 19:31, Richard Thomas wrote: On Jun 7, 10:17 am, Peter Otten __pete...@web.de wrote: Alfred Bovin wrote: I'm working on something where I need to read a (binary) file bit by bit and do something depending on whether the bit is 0 or 1. Any help on doing the actual file reading is

Re: Reading file bit by bit

2010-06-07 Thread Peter Otten
Richard Thomas wrote: On Jun 7, 10:17 am, Peter Otten __pete...@web.de wrote: Alfred Bovin wrote: I'm working on something where I need to read a (binary) file bit by bit and do something depending on whether the bit is 0 or 1. Any help on doing the actual file reading is appreciated.

Re: Reading file bit by bit

2010-06-07 Thread Nobody
On Mon, 07 Jun 2010 02:31:08 -0700, Richard Thomas wrote: You're reading those bits backwards. You want to read the most significant bit of each byte first... Says who? There is no universal standard for bit-order. Among bitmap image formats, XBM is LSB-first while BMP and PBM are MSB-first.

Re: Reading file bit by bit

2010-06-07 Thread Ulrich Eckhardt
Nobody wrote: On Mon, 07 Jun 2010 02:31:08 -0700, Richard Thomas wrote: You're reading those bits backwards. You want to read the most significant bit of each byte first... Says who? Says Python: bin(192) '0x1100' That said, I totally agree that there is no inherently right way and

Re: Reading file bit by bit

2010-06-07 Thread Peter Otten
Ulrich Eckhardt wrote: Nobody wrote: On Mon, 07 Jun 2010 02:31:08 -0700, Richard Thomas wrote: You're reading those bits backwards. You want to read the most significant bit of each byte first... Says who? Says Python: bin(192) '0x1100' Hmm, if that's what /your/ Python says,

Re: Reading file bit by bit

2010-06-07 Thread Ulrich Eckhardt
Peter Otten wrote: Ulrich Eckhardt wrote: Says Python: bin(192) '0x1100' Hmm, if that's what /your/ Python says, here's mine to counter: bin(192) '0_totally_faked_binary_0011' Argh! Of course one of my Pythons says '0b1100' and not what I mistyped above =( Uli *goes

Re: Reading file bit by bit

2010-06-07 Thread superpollo
Ulrich Eckhardt ha scritto: Peter Otten wrote: Ulrich Eckhardt wrote: Says Python: bin(192) '0x1100' Hmm, if that's what /your/ Python says, here's mine to counter: bin(192) '0_totally_faked_binary_0011' Argh! Of course one of my Pythons says '0b1100' and not what I

Re: Reading file bit by bit

2010-06-07 Thread Ulrich Eckhardt
superpollo wrote: mine goes like this: bin(192) Traceback (most recent call last): File stdin, line 1, in module NameError: name 'bin' is not defined Yep, one of mine, too. The bin function was new in 2.6, as were binary number literals (0b1100). Uli -- Sator Laser GmbH

Re: Reading file bit by bit

2010-06-07 Thread Grant Edwards
On 2010-06-07, Richard Thomas chards...@gmail.com wrote: You're reading those bits backwards. You want to read the most significant bit of each byte first... Can you explain the reasoning behind that assertion? -- Grant Edwards grant.b.edwardsYow! I can't decide which

Re: Reading file bit by bit

2010-06-07 Thread Terry Reedy
On 6/7/2010 6:20 AM, Ulrich Eckhardt wrote: Ulrich Eckhardt wrote: data = f.read() for byte in data: for i in range(8): bit = 2**i byte ... Correction: Of course you have to use ord() to get from the single-element string (byte above) to its integral