Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading binary big endian files in python

I'd like to use python read a large binary file in ieee big endian 64bit floating point format, but am having trouble getting the correct values. I have a working method in matlab, as below:

fid=fopen(filename,'r','ieee-be');
data=fread(fid,inf,'float64',0,'ieee-be');
fclose(fid)

I've tried the following in python:

data = np.fromfile(filename, dtype='>f', count=-1)

This method doesn't throw any errors, but the values it reads are extremely large and incorrect. Can anyone help with a way to read these files? Thanks in advance.

like image 996
Connor Avatar asked Nov 20 '25 21:11

Connor


1 Answers

Using >f will give you a single-precision (32-bit) floating point value. Instead, try

data = np.fromfile(filename, dtype='>f8', count=-1)
like image 52
ilmarinen Avatar answered Nov 22 '25 11:11

ilmarinen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!