简体   繁体   English

使用python从二进制文件中读取数字数据

[英]Read numeric data from binary file using python

I would like to read a binary data file that contains a header part (text) and then a numeric array. 我想读取一个二进制数据文件,其中包含标题部分(文本),然后是数字数组。 I can use f.read(block_size) to keep streaming in the header part, but what is the best way to read the numeric array? 我可以使用f.read(block_size)在标题部分保持流式传输,但读取数值数组的最佳方法是什么?

In MatLab, I could do 在MatLab,我能做到

fid = fopen(data_file_name, 'rb');
line = fread(fid, block_size, '*char'); 
data = fread(fid, 'long');

In Python, what I have done is 在Python中,我所做的是

f = open(data_file_name, 'rb')
header = f.read(block_size)

and from here I do not know how to get to the numeric array. 从这里我不知道如何到达数字数组。

You can use struct.unpack to unpack the numeric data. 您可以使用struct.unpack来解压缩数字数据。

eg 例如

with open('file','rb') as fin:
    header = fin.read(header_size)
    data_str = fin.read(num_data_bytes)
    data_tuple = struct.unpack('100f',data_str)  #100 4-byte floats

Depending on the data, you can read it directly to a numpy array using numpy.fromfile . 根据数据,您可以使用numpy.fromfile其直接读取到numpy数组。 That function accepts an open file object, so you can read the header and then pass the open file object in so numpy can read the data. 该函数接受一个打开的文件对象,因此您可以读取标题然后传递打开的文件对象,因此numpy可以读取数据。 In this question , I asked about the details of reading binary data from a string into a numpy array. 这个问题中 ,我询问了将字符串中的二进制数据读入numpy数组的详细信息。 It's a slightly different problem, but much of the answer there applies to this as well (how to specify endianness, etc.) 这是一个稍微不同的问题,但是大部分答案也适用于此(如何指定字节顺序等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM