[英]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. 我有一些包含4字节整数的二进制文件(有些可能很大(100MB))。
Can anyone supply a code snippet to show how to extract each 4 byte integer until the end of the file is reached? 任何人都可以提供代码片段来展示如何提取每个4字节整数,直到文件结束? Using Python 2.7.
使用Python 2.7。
Thanks 谢谢
You could use struct.unpack()
: 你可以使用
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. 这使用
<I
将字节解释为little-endian无符号整数。 Adjust the format as needed; 根据需要调整格式;
>
for big-endian, i
for signed integers. >
对于big-endian, i
用于签名整数。
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: 如果您需要一次读取大量的整数值并知道需要读取的数量,请查看
array
模块 :
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: 如果文件的
array.byteswap()
与您的系统不匹配,您需要使用array.byteswap()
:
if sys.byteorder != 'little':
arr.byteswap()
Check out the NumPy fromfile
function . 查看NumPy
fromfile
函数 。 You provide a simple type annotation about the data to be read, and the function efficiently reads it into a NumPy ndarray
object. 您提供有关要读取的数据的简单类型注释,并且该函数有效地将其读入NumPy
ndarray
对象。
import numpy as np
np.fromfile(file_name, dtype='<i4')
You can change dtype
to reflect size and byte order as well. 您也可以更改
dtype
以反映大小和字节顺序。 See here for some examples. 请看这里的一些例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.