Reading binary file with python without knowing structure

前端 未结 1 1908
南笙
南笙 2021-01-27 16:28

I have a binary file containing the position of 8000 particles. I know that each particle value should look like \"-24.6151...\" (I don\'t know with which precision the values

相关标签:
1条回答
  • 2021-01-27 17:19

    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

    0 讨论(0)
提交回复
热议问题