convert binary to float

Mason john.gerald.mason at gmail.com
Sun Jun 1 21:55:45 CEST 2008


I have tried and tried...

I'd like to read in a binary file, convert it's 4 byte values into
floats, and then save as a .txt file.

This works from the command line (import struct);

    In [1]: f = open("test2.pc0", "rb")
    In [2]: tagData = f.read(4)
    In [3]: tagData
    Out[3]: '\x00\x00\xc0@'

I can then do the following in order to convert it to a float:

    In [4]: struct.unpack("f", "\x00\x00\xc0@")
    Out[4]: (6.0,)

But when I run the same code from my .py file:

    f = open("test2.pc0", "rb")
    tagData = f.read(4)
    print tagData

I get this (ASCII??):
└@

I only know how to work with '\x00\x00\xc0@'.

I don't understand why the output isn't the same. I need a solution
that will allow me to convert my binary file into floats. Am I close?
Can anyone point me in the right direction?

Thanks,
Mason







More information about the Python-list mailing list