Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading 4 byte integers from binary file in Python

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

like image 262
bzo Avatar asked Mar 06 '14 15:03

bzo


People also ask

How do I read a large binary file in Python?

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() ...

How do you read binary numbers in Python?

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.

How do you convert binary to text in Python?

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.


1 Answers

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()
like image 127
Martijn Pieters Avatar answered Sep 28 '22 07:09

Martijn Pieters