2

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?

In MatLab, I could do

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

In Python, what I have done is

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.

Brian Tompsett - 汤莱恩
5,92772 gold badges64 silver badges135 bronze badges
asked Apr 2, 2013 at 14:47

1 Answer 1

5

You can use struct.unpack to unpack the numeric data.

e.g.

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. 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. In this question, I asked about the details of reading binary data from a string into a numpy array. It's a slightly different problem, but much of the answer there applies to this as well (how to specify endianness, etc.)

answered Apr 2, 2013 at 14:48
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.