Search code examples
python-3.xbinaryfiles

How to read a binary file and write it in a txt or csv file using python?


I have a binary file (.man), containing data that I want to read, using python 3.7. The idea is to convert this binary file into a txt or a csv file.

I know the total number of values in the binary file but not the number of bytes per value.

I have red many post talking about binary file but none was helpful...

Thank you in advance,


Solution

  • The solution I found is that:

        import struct
        import numpy as np
    
        data =[]
    
        with open('binary_file', "rb") as f:
            while len(data)<length(binary_file):
                data.extend([struct.unpack('f',f.read(4))])
    

    Of course, this works because I know that the encoding is simple precision.