Reading binary file with python without knowing structure

依然范特西╮ 提交于 2019-12-02 11:36:21

What you are printing now are the bytes composing your floating point data. So it doesn't make sense as numerical values.

Of course, there's no 100% sure answer since we didn't see your data, but I'll try to guess:

You have 8000 values to read and the file size is 64000. So you probably have double IEEE values (8 bytes each). If it's not IEEE, then you're toast.

In that case you could try the following:

import struct
with open('.//results0epsilon/energybinary/energy_00004.dat', 'br') as f:
    buffer = f.read()
    print ("Length of buffer is %d" % len(buffer))

    data = struct.unpack("=8000d",buffer)

if the data is printed bogus, it's probably an endianness problem. So change the =8000 by <8000 or >8000.

for reference and packing/unpacking formats: https://docs.python.org/3/library/struct.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!