- Convert binary string to bytearray in Python 3
- Python read a binary file (Examples)
- Python read a binary file
- Python read a binary file to an array
- Python read a binary file into a byte array
- Python read a binary file line by line
- Python read a binary file to Ascii
- Python read a binary file into a NumPy array
- Python read a binary file into CSV
Convert binary string to bytearray in Python 3
You have to either convert it to an int and take 8 bits at a time, or chop it into 8 byte long strings and then convert each of them into ints. In Python 3, as PM 2Ring and J.F Sebastian’s answers show, the to_bytes() method of int allows you to do the first method very efficiently. This is not available in Python 2, so for people stuck with that, the second method may be more efficient. Here is an example:
>>> s = "0110100001101001" >>> bytes(int(s[i : i + 8], 2) for i in range(0, len(s), 8)) b'hi'
To break this down, the range statement starts at index 0, and gives us indices into the source string, but advances 8 indices at a time. Since s is 16 characters long, it will give us two indices:
>>> list(range(0, 50, 8)) [0, 8, 16, 24, 32, 40, 48] >>> list(range(0, len(s), 8)) [0, 8]
(We use list() here to show the values that will be retrieved from the range iterator in Python 3.)
We can then build on this to break the string apart by taking slices of it that are 8 characters long:
>>> [s[i : i + 8] for i in range(0, len(s), 8)] ['01101000', '01101001']
Then we can convert each of those into integers, base 2:
>>> list(int(s[i : i + 8], 2) for i in range(0, len(s), 8)) [104, 105]
And finally, we wrap the whole thing in bytes() to get the answer:
>>> bytes(int(s[i : i + 8], 2) for i in range(0, len(s), 8)) b'hi'
>>> zero_one_string = "0110100001101001" >>> int(zero_one_string, 2).to_bytes((len(zero_one_string) + 7) // 8, 'big') b'hi'
It returns bytes object that is an immutable sequence of bytes. If you want to get a bytearray — a mutable sequence of bytes — then just call bytearray(b’hi’) .
Here’s an example of doing it the first way that Patrick mentioned: convert the bitstring to an int and take 8 bits at a time. The natural way to do that generates the bytes in reverse order. To get the bytes back into the proper order I use extended slice notation on the bytearray with a step of -1: b[::-1] .
def bitstring_to_bytes(s): v = int(s, 2) b = bytearray() while v: b.append(v & 0xff) v >>= 8 return bytes(b[::-1]) s = "0110100001101001" print(bitstring_to_bytes(s))
Clearly, Patrick’s second way is more compact. 🙂
However, there’s a better way to do this in Python 3: use the int.to_bytes method:
def bitstring_to_bytes(s): return int(s, 2).to_bytes((len(s) + 7) // 8, byteorder='big')
If len(s) is guaranteed to be a multiple of 8, then the first arg of .to_bytes can be simplified:
return int(s, 2).to_bytes(len(s) // 8, byteorder='big')
This will raise OverflowError if len(s) is not a multiple of 8, which may be desirable in some circumstances.
Another option is to use double negation to perform ceiling division. For integers a & b, floor division using //
gives the integer n such that
n Eg,
47 // 10 gives 4, and
-(-47 // 10) gives 5, effectively performing ceiling division.
Thus in bitstring_to_bytes we could do:
return int(s, 2).to_bytes(-(-len(s) // 8), byteorder='big')
However, not many people are familiar with this efficient & compact idiom, so it’s generally considered to be less readable than
return int(s, 2).to_bytes((len(s) + 7) // 8, byteorder='big')
Python read a binary file (Examples)
In this Python tutorial, we will learn how to read a binary file in python, and also we will cover these topics:
- How to read a binary file to an array in Python
- How to read a binary file into a byte array in Python
- How to read a binary file line by line in Python
- Python read a binary file to Ascii
- How to read a binary file into a NumPy array in Python
- How to read a binary file into CSV in Python
Python read a binary file
Here, we will see how to read a binary file in Python.
- Before reading a file we have to write the file. In this example, I have opened a file using file = open(“document.bin”,”wb”) and used the “wb” mode to write the binary file.
- The document.bin is the name of the file.
- I have taken a variable as a sentence and assigned a sentence “This is good”, To decode the sentence, I have used sentence = bytearray(“This is good”.encode(“ascii”)).
- And to write the sentence in the file, I have used the file.write() method.
- The write() is used to write the specified text to the file. And then to close the file, I have used the file.close().
Example to write the file:
file = open("document.bin","wb") sentence = bytearray("This is good".encode("ascii")) file.write(sentence) file.close()
- To read the file, I have taken the already created file document.bin and used the “rb” mode to read the binary file.
- The document.bin is the file name. And, I have using the read() method. The read() method returns the specified number of bytes from the file.
file = open("document.bin","rb") print(file.read(4)) file.close()
In this output, you can see that I have used print(file.read(4)). Here, from the sentence, it will read only four words. As shown in the output.
Python read a binary file to an array
Here, we can see how to read a binary file to an array in Python.
- In this example, I have opened a file as array.bin and used the “wb” mode to write the binary file. The array.bin is the name of the file.
- And assigned an array as num=[2,4,6,8,10] to get the array in byte converted format, I have used bytearray(). The bytearray() method returns the byte array objects.
- To writes the array in the file, I have used the file.write(). And file.close() to close the file.
Example to write an array to the file:
file=open("array.bin","wb") num=[2,4,6,8,10] array=bytearray(num) file.write(array) file.close()
- To read the written array from the file, I have used the same file i.e,file=open(“array.bin”,”rb”).
- The “rb” mode is used to read the array from the file.
- The list() function is used to create the list object number=list(file.read(3)). The file.read() is used to read the bytes from the file.
- The file.read(3) is used to read-only three numbers from the array. The file.close() is used to close the file.
Example to read an array from the file:
file=open("array.bin","rb") number=list(file.read(3)) print (number) file.close()
To get the output, I have used print(number). And to close the file, I have used file.close(). In the below screenshot you can see the output.
- How to Convert Python string to byte array with Examples
- Python Array with Examples
- Create an empty array in Python
Python read a binary file into a byte array
Now, we can see how to read a binary file into a byte array in Python.
- In this example, I have opened a file called sonu.bin and “rb” mode is used to read a binary file, and sonu.bin is the name of the file. Here, I have stored some data in the sonu.bin file.
- The byte = file.read(3) is used to read the file, and file.read(3) is used to read only 3 bytes from the file.
- The while loop is used to read and iterate all the bytes from the file.
file = open("sonu.bin", "rb") byte = file.read(3) while byte: print(byte) byte = file.read(3)
To read the byte from the file, I have used print(byte). You can refer to the below screenshot for the output.
Python read a binary file line by line
Here, we can see how to read a binary file line by line in Python.
- In this example, I have taken a line as lines=[“Welcome to python guides\n”] and open a file named as file=open(“document1.txt”,”wb”) document1.txt is the filename.
- The “wb” is the mode used to write the binary files. The file.writelines(lines) is used to write the lines from the file.
- The writelines() returns the sequence of string to the file. The file.close() method is used to close the file.
Example to write the file:
lines=["Welcome to python guides\n"] file=open("document1.txt","wb") file.writelines(lines) file.close()
- To read the written file, I have used the same filename as document1.txt, I have used file=open(“document1.txt”,”rb”) to open the file, “rb” mode is used to read the binary file and, To read the line from the file I have used line=file.readline().
- The readline() returns one line from the file.
file=open("document1.txt","rb") line=file.readline() print(line) file.close()
To get the output, print(line) is used and lastly to close the file, I have used file.close().
Python read a binary file to Ascii
Now, we can see how to read a binary file to Ascii in Python.
- In this example, I have opened a file named test.bin using file = open(‘test.bin’, ‘wb’), The ‘wb’ mode is used to write the binary file and I have taken a variable as a sentence and assigned a sentence = ‘Hello Python’. To encode the sentence.
- I have used file_encode = sentence.encode(‘ASCII’). To write the encoded sentence in the file, I have used the file.write(file_encode).
- The file.seek() method returns the new position. To read the written file, I have used the file.read() which returns a byte from the file.
- And then to convert the binary sentence into Ascii, I have used new_sentence = bdata. decode(‘ASCII’).
file = open('test.bin', 'wb') sentence = 'Hello Python' file_encode = sentence.encode('ASCII') file.write(file_encode) file.seek(0) bdata = file.read() print('Binary sentence', bdata) new_sentence = bdata.decode('ASCII') print('ASCII sentence', new_sentence)
To get the output as an encoded sentence, I have used print(‘ASCII sentence’, new_sentence). You can refer to the below screenshot for the output.
Python read a binary file into a NumPy array
Here, we can see how to read a binary file into a numpy array in Python.
- In this example, I have imported a module called NumPy. The array = np.array([2,8,7]) is used to create an array, The .tofile is used to write all the array to the file. The array.bin is the name of the binary file.
- The np.fromfile is used to construct an array from the data in the file. The dtype=np.int8 is the datatype object. The output of the array changes if we change np.int8 to int32 or int64.
import numpy as np array = np.array([2,8,7]).tofile("array.bin") print(np.fromfile("array.bin", dtype=np.int8))
To get the output, I have used print(np.fromfile(“array.bin”, dtype=np.int8)). The below screenshot shows the output.
Python read a binary file into CSV
Here, we can see how to read binary file into csv in Python.
- In this example, I have imported a module called CSV. The CSV module is a comma-separated value module. It is used to read and write tabular data in CSV format.
- I have opened a file called lock.bin and “w” mode is used to write the file writer = csv.writer(f) is used to write the objects in the file. The lock.bin is the name of the file.
- The writer() returns the write object which converts data into a string.
- The writer.writerows is used to write all the rows into the file. To close the file, f.close() is used.
Example to write the csv file:
import csv f = open("lock.bin", "w") writer = csv.writer(f) writer.writerows([["a", 1], ["b", 2], ["c", 3], ["d",4]]) f.close()
To read the CSV file, I have opened the file lock.bin in which data is already written, The ‘r‘ mode is used to read the file. To read the CSV file, I have used reader = csv.reader(file) to return a list of rows from the file.
Example to read the csv file:
import csv with open('lock.bin', 'r') as file: reader = csv.reader(file) for row in reader: print(row)
To get the output I have used print(row). The below screenshot shows the output.
You may like the following Python tutorials:
In this tutorial we have learned about Python read a binary file, also we have covered these topics:
- Python read a binary file to an array
- Python read a binary file into a byte array
- Python read a binary file line by line
- Python read a binary file to Ascii
- Python read a binary file into a NumPy array
- Python read a binary file into CSV
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.