- Beginner: How do I read in one bit/byte at a time from a file?
- Recommended Answers Collapse Answers
- All 14 Replies
- Working with Binary Data in Python
- Encoding
- Python and Bytes
- Using Python How can I read the bits in a byte?
- Using Python How can I read the bits in a byte?
- Using Twain Module in Python
- Network Scanner in Python
Beginner: How do I read in one bit/byte at a time from a file?
Hello, I have just started learning Python. Please can you tell me if there is a way to read in a file one bit or byte at a time (i.e. in binary). Thank you!
- 5 Contributors
- 14 Replies
- 6K Views
- 1 Day Discussion Span
- Latest Post 14 Years Ago Latest Post by jlm699
Recommended Answers Collapse Answers
If you have Python three, then this is easy:
with open(filename, "rb") as fh: b = True while b: b = fh.read(1) #. do something with b
Thank you, however I’m using Python 2.6.1. is there a different way to do it using that?
Maybe try this.. pretty much same principal as 3.0 :
fh = open('myfile.txt', 'rb') while True: try: fh.read(1) except: pass
Here’s how to add items to a list:
>>> bits_list = [] >>> bits_list.append('0') >>> bits_list.append('1') >>> bits_list.append('1') >>> bits_list ['0', '1', '1'] >>>
Yes well, I forgot that read() does not raise a stop iteration when used in the manner that it is being used here. so the try-except case never gets thrown and we’re stuck in an infinite loop.
You can do something like, if bit == »: break
All 14 Replies
with open(filename, "rb") as fh: b = True while b: b = fh.read(1) #. do something with b
fh = open('myfile.txt', 'rb') while True: try: fh.read(1) except: pass
To be more specific, I want to read in a bit at a time from a file, and add each individual bit to a list. Can you help with this? (sorry, I am a complete beginner!)
>>> bits_list = [] >>> bits_list.append('0') >>> bits_list.append('1') >>> bits_list.append('1') >>> bits_list ['0', '1', '1'] >>>
Yeah, that’s what I thought. But this is the code I have — it does not seem to work, just prints out a blank line in the shell then RESTART. And then the whole shell/IDLE seemed to crash when I closed them. The file I’m trying to read one bit at a time from is an encrypted jpeg. This is the code I have:
encrypted = [] fh = open('encjpgfile', 'rb') while True: try: bit = fh.read(1) #print bit encrypted.append(bit) except: pass print encrypted sys.exit(0)
Yes well, I forgot that read() does not raise a stop iteration when used in the manner that it is being used here. so the try-except case never gets thrown and we’re stuck in an infinite loop. You can do something like, if bit == »: break
that works great, thank you! Only thing is, when I print out the ‘encrypted’ list, it’s full of characters like ‘\x0e’, ‘\xc4’ etc etc. not the 1s and 0s I want, how do I get it to do this? Sorry for all the questions, very limited knowledge I have, you’ve been very helpful though.
from binascii import hexlify L = ["00", "01", "10", "11"] LL = [u+v for u in L for v in L] s = "0123456789abcdef" D = dict((s[i], LL[i]) for i in range(16)) jpeg = open('encjpgfile', 'rb').read() bits = ''.join(D[x] for x in hexlify(jpeg)) print(bits)
# can be used for Python25/26 def int2bin(num, bits): """ returns the binary of integer num, using bits number of digits, will pad with leading zeroes """ bs = '' for x in range(0, bits): if 2**x == 2**x & num: bs = '1' + bs else: bs = '0' + bs return bs image_file = "red.jpg" # a testfile try: # read all data into a string data = open(image_file, 'rb').read() except IOError: print "Image file %s not found" % image_file raise SystemExit bit_list = [] # create a list padded bits for ch in data: # take the int value of ch and convert to 8 bit strings bits8 = int2bin(ord(ch), 8) bit_list.append(bits8) print(bit_list) """ my result --> ['11111111', '11011000', '11111111', '11100000', '00000000', . ] """
that works great, thank you! Only thing is, when I print out the ‘encrypted’ list, it’s full of characters like ‘\x0e’, ‘\xc4’ etc etc. not the 1s and 0s I want, how do I get it to do this? Sorry for all the questions, very limited knowledge I have, you’ve been very helpful though.
Yes, unfortunately Python’s file handling wasn’t intended for bit-by-bit reading of a file; the read function takes an optional parameter for the number of bytes to read. Since we used ‘1’, that told Python to read the file byte-by-byte. Each of the characters that you see is a single byte of the image data. Equipped with the knowledge that a byte consists of 8 bits, you could consider it a personal challenge to come up with a way to take each of those bytes and translate it into the corresponding bits. \xc4 means hexadecimal C4, a.k.a. 0xC4
>>> int('c4',16) 196 >>> int('0xc4',16) 196 >>>
In fact, I believe that Python 3.0 (and therefore 2.6) has a built-in method bin() that would translate directly to binary.
Working with Binary Data in Python
Alright, lets get this out of the way! The basics are pretty standard:
- There are 8 bits in a byte
- Bits either consist of a 0 or a 1
- A byte can be interpreted in different ways, like binary octal or hexadecimal
Note: These are not character encodings, those come later. This is just a way to look at a set of 1’s and 0’s and see it in three different ways(or number systems).
Input : 10011011 Output : 1001 1011 ---- 9B (in hex) 1001 1011 ---- 155 (in decimal) 1001 1011 ---- 233 (in octal)
This clearly shows a string of bits can be interpreted differently in different ways. We often use the hex representation of a byte instead of the binary one because it is shorter to write, this is just a representation and not an interpretation.
Encoding
Now that we know what a byte is and what it looks like, let us see how it is interpreted, mainly in strings. Character Encodings are a way to assign values to bytes or sets of bytes that represent a certain character in that scheme. Some encodings are ASCII(probably the oldest), Latin, and UTF-8(most widely used as of today. In a sense encodings are a way for computers to represent, send and interpret human readable characters. This means that a sentence in one encoding might become completely incomprehensible in another encoding.
Python and Bytes
From a developer’s point of view, the largest change in Python 3 is the handling of strings. In Python 2, the str type was used for two different kinds of values – text and bytes, whereas in Python 3, these are separate and incompatible types. This means that before Python3 we could treat a set of bytes as a string and work from there, this is not the case now, now we have a separate data type, called bytes. This data type can be briefly explained as a string of bytes, which essentially means, once the bytes data type is initialized it is immutable.
Using Python How can I read the bits in a byte?
The target server must have open ports that can accept and initiate new connections. In Matlab I can read the byte bit by bit with , and then retrieve each bit by , etc.
Using Python How can I read the bits in a byte?
I have a file where the first byte contains encoded information. In Matlab I can read the byte bit by bit with var = fread(file, 8, ‘ubit1’) , and then retrieve each bit by var(1), var(2) , etc.
Is there any equivalent bit reader in python?
Read the bits from a file, low bits first.
def bits(f): bytes = (ord(b) for b in f.read()) for b in bytes: for i in xrange(8): yield (b >> i) & 1 for b in bits(open('binary-file.bin', 'r')): print b
The smallest unit you’ll be able to work with is a byte. To work at the bit level you need to use bitwise operators.
x = 3 #Check if the 1st bit is set: x&1 != 0 #Returns True #Check if the 2nd bit is set: x&2 != 0 #Returns True #Check if the 3rd bit is set: x&4 != 0 #Returns False
With numpy it is easy like this:
Bytes = numpy.fromfile(filename, dtype = "uint8") Bits = numpy.unpackbits(Bytes)
More info here:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html
You won’t be able to read each bit one by one — you have to read it byte by byte. You can easily extract the bits out, though:
f = open("myfile", 'rb') # read one byte byte = f.read(1) # convert the byte to an integer representation byte = ord(byte) # now convert to string of 1s and 0s byte = bin(byte)[2:].rjust(8, '0') # now byte contains a string with 0s and 1s for bit in byte: print bit
Network Scanner in Python, To get the list of the available hosts on a network, there are two basic methods – ICMP Echo Request It is also known by using ‘ping command’. An ICMP packet is sent to a host using the IP address and if the ICMP echo is received, that means that the host is online and is receiving the signals.
Using Twain Module in Python
I have 64 bit Windows and 64 bit Python . In the twain docs said : supports only 32 bit . I have used twain data source to install my 64 bit machine and used this code to connect scanner device:
import twain sm = twain.SourceManager(0) ss = sm.OpenSource() ss.RequestAcquire(0,0) rv = ss.XferImageNatively() if rv: (handle, count) = rv twain.DIBToBMFile(handle, 'image.bmp')
When I run the app. a windows opens like :
so why this window is empty . The scanner device will show in this window ?
The driver files for a 32 bit driver will be placed twain_32 folder and 64 bit driver files under the twain_64 folder. The twain DSM which searches for the drivers available has two versions 32 and 64 bit. The 32 bit version of the DSM looks for the drivers in the twain_32 folder and 64 bit version under the 64 bit version of the folder. Hence, it looks like you need to check which version of the driver is installed (32/64 bit). Dependency walker will help you quickly resolve this issue. More details on dependencies — Refer to chapter 12 — https://www.twain.org/wp-content/uploads/2016/03/TWAIN-2.2-Spec.pdf
Bit operations python Code Example, Dart queries related to “bit operations python” xor symbol pyton; python binary or; ngation ~~~~5 in python; how to scan bits in python; OR bitwise python; complement in python; shift left operator in python; python lists 3.9.1; what does the bitwise & python operation do; shift byte python; bit manipulation with xor …
Network Scanner in Python
A network scanner is one major tool for analyzing the hosts that are available on the network. A network scanner is an IP scanner that is used for scanning the networks that are connected to several computers.
To get the list of the available hosts on a network, there are two basic methods –
ICMP Echo Request
It is also known by using ‘ping command’. An ICMP packet is sent to a host using the IP address and if the ICMP echo is received, that means that the host is online and is receiving the signals. For this, it necessary to get all the IP addresses for which you wish to test that the host is connected or not. This method works on the assumption that network devices have ICMP enabled.