0

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")

1 Answer 1

1

Its just a string form of a tuple.

unpack(...)

Gives back a tuple.This is stated in the docs:

... The result is a tuple even if it contains exactly one item.

If you want the first element:

first = unpack('f', byte)[0]

In your code, use:

output_file.write(str(unpack('f', byte)[0]))

Tip: Use the csv module.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.