Home > database >  Not able to read the correct values from a binary file in python
Not able to read the correct values from a binary file in python

Time:04-20

I created and wrote double (data type) values into a binary file. The file does not contain any headers. It is just raw data written into the file using QDataStream in Qt. I am trying to read the values in a python (version 3.6.9) script, however, the data that is read in the python script is not corect. The same file when read in c gives the correct data values. My python code is :

with open("cont_points.dat", mode='rb') as f:
    controlP=np.fromfile(f,dtype=np.double)
print(controlP)

The last print statement gives the following result, which i assume are all zeros:

[2.11855e-320 1.04347e-320 0.00000e 000 3.03865e-319 2.11855e-320
 2.05531e-320 0.00000e 000 3.03865e-319 1.61263e-320 1.04347e-320
 ... ... ...
 2.05531e-320 0.00000e 000 3.03865e-319 2.05531e-320 1.04347e-320
 0.00000e 000 3.03865e-319 2.05531e-320 2.05531e-320 0.00000e 000
 3.03865e-319]

the data in the file is [-4, 3, 0, 1, -4, 4, 0, 1, -3.5, 3, 0, 1,.... so on]. In total there are 136 values that i want to read. As i get the correct result when i read the same file in c , I think the problem is in my python code. Can anyone please tell what am I doing wrong here?

CodePudding user response:

The problem is the endianness of the data in the file. QDataStream by default uses big endian byte order. On your system numpy uses little endian. You can read the file using dtype='>f8'.

This will result in an array with non-native byte order (controlP.dtype.isnative will be False). Depending on what happens next you might want to call .byteswap() on it first for better performance. See also this page in the numpy docs.

  • Related