convert binary to float

Mark Tolonen M8R-yfto6h at mailinator.com
Sun Jun 1 17:59:07 EDT 2008


>"George Sakkis" <george.sakkis at gmail.com> wrote in message 
>news:829b1e8f-baac-4ff4-909b->b39df97a436c at d45g2000hsc.googlegroups.com...
>On Jun 1, 3:55 pm, Mason <john.gerald.ma... at gmail.com> wrote:
>> 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??):
>> „@
>
> Remembering to put that struct.unpack() call in your module might
> help ;-)
>
> George

tagData still contains your data, but it is being displayed two different 
ways.  Consult the documentation about str() and repr().

-Mark




More information about the Python-list mailing list