Home:ALL Converter>Reading from binary as float(4 bytes per number) to csv file

Reading from binary as float(4 bytes per number) to csv file

Ask Time:2018-05-22T20:49:04         Author:John Dign

Json Formatter

I would like to read from a binary as float values and write them to a csv file, which almost works with the code below. The thing is struct.unpack is writing the float value like:

(number,)

and I would like to write it without the paranteses,().

Is there a better way to get the values as a float instead of using unpack or what do you suggest?

count = 0
output_file = open(r"C:\Users\heltbork\Desktop\binTocsvDirect\00000006.txt", "w")

with open(r"C:\Users\heltbork\Desktop\binTocsvDirect\00000006.bin", "rb") as f:
    while True:
        byte = f.read(4)
        if not byte:
            break
        output_file.write(str(unpack('f', byte)))
        count = count + 1
        if count == 6:
            count = 0
            output_file.write("\n")

Author:John Dign,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50468163/reading-from-binary-as-float4-bytes-per-number-to-csv-file
yy