Reading 4 byte integers from binary file in Python

后端 未结 2 802
甜味超标
甜味超标 2021-02-06 12:55

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

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-06 13:22

    You could use struct.unpack():

    with open(filename, 'rb') as fileobj:
        for chunk in iter(lambda: fileobj.read(4), ''):
            integer_value = struct.unpack('

    This uses 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()
    

提交回复
热议问题