I have a some sets of binary files (some are potentially large (100MB)) that contain 4 byte integers.
Can anyone supply a code snippet to show how to extract each 4 byte integer until the end of the file is reached? Using Python 2.7.
Thanks
To read from a binary file, we need to open it with the mode rb instead of the default mode of rt : >>> with open("exercises. zip", mode="rb") as zip_file: ... contents = zip_file. read() ...
In Python, you can simply use the bin() function to convert from a decimal value to its corresponding binary value. And similarly, the int() function to convert a binary to its decimal value. The int() function takes as second argument the base of the number to be converted, which is 2 in case of binary numbers.
Method #1: The binary data is divided into sets of 7 bits because this set of binary as input, returns the corresponding decimal value which is ASCII code of the character of a string. This ASCII code is then converted to string using chr() function.
You could use struct.unpack()
:
with open(filename, 'rb') as fileobj:
for chunk in iter(lambda: fileobj.read(4), ''):
integer_value = struct.unpack('<I', chunk)[0]
This uses <I
to interpret the bytes as little-endian unsigned integers. Adjust the format as needed; >
for big-endian, i
for signed integers.
If you need to read a lot of integer values in one go and know how many you need to read, take a look at the array
module as well:
from array import array
arr = array('L')
with open(filename, 'rb') as fileobj:
arr.fromfile(fileobj, number_of_integers_to_read)
where you'd need to use array.byteswap()
if the endianess of the file and your system didn't match:
if sys.byteorder != 'little':
arr.byteswap()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With