noob stuck on reading double

Hannah Drayson hannah.drayson at plymouth.ac.uk
Tue Jan 29 13:35:30 EST 2008


Hi all,

I have a .bin file which python just won't play ball with-
Does anyone know what I'm doing wrong- is it simply incompatible?

I've read it fine using a C program - its 113 doubleword fields- apparently its possible to handle these in python in a very similar way to C.
I can provide the c code and have attached .bin if anyone is interested...

Thank you all.



It imports as a string of rubbish...
i.e.

<open file 'data.bin', mode 'rb' at 0x5a2a8>
>>> text = f.read()
>>> print text
?F?C??y??>?
@?ӜI at g[??B8~??????Q???Q???Q???Q???Q???Q???Q???Q???Q???Q????=N@???????????????????/???8@@@@?Q at E??/??T at N#????S@?????Q???Q???Q???Q???Q??Ǒ????????R[???Q???????




It won't unpack using struct...

>>> unpack('d', text)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/struct.py", line 87, in unpack
    return o.unpack(s)
struct.error: unpack requires a string argument of length 8





Using  str and repr gets me something that looks like unicode...

>>> str(text)
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\xa0F\xa2C\xc0\xd2y\xf9\xf8>\xd2\n@\x00\x00\x00\xc0\xd3\x9cI@\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00g[\xa0?B8~\xd4\x00\x00\x00
\x00\x00\x00\xf8\xff\x00\x00\x00\x00\x00\x00\xf8\xff\x00\x00\x00\x00\x00\x00\x0
0\x00\x00\x00\x00\x00\x00\x00\
etc.....




Tried formatting it...

>>> str(text) % ('Python', 'g')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting





Tried converting it to unicode...


>>> unicode(text, 'utf8', 'strict')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa0 in position 19: unexpected code byte





I even tried chopping it up into 8 character pieces and then converting it into a float, but I think I'm just being stupid by this point...


>>> the_list=[]
>>> index = 0
>>> second_dig = 7
>>> while index < (len(bum))/8:
...     new_bit = bum[index:second_dig]
...     the_list.append(new_bit)
...     index += 8
...     second_dig += 8
...
>>> print the_list
[u'\x00\x00\x00\x00\x00\x00\x00', u'\x00\x00\x00\x00\x00\x00\x00', u'\x00\x00\x00\ufffdF\ufffdC', u'y\ufffd@\x00\x00\x00\ufffd', u'I@\x00\x00\x00\x00\x00', u'\x00\x00\x00\x01\x00\x00\x00', u'\x00\x00g[\ufffd?B', u'~\ufffd\x00\x00\x00\x00\x00', u'\x00\x00\x00\ufffd\x00\x00\x00', u'\x00\x00\x00\x00\x00\x00\x00', u'\x00\x00\x00\x00\x00\x00\x00', u'\x00\x00\x00\x00\x00\x00\x00', u'\x00\x00\x00\x00\x00\x00\x00']
>>> the_list[1]
u'\x00\x00\x00\x00\x00\x00\x00'
>>> float(the_list[1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'decimal' codec can't encode character u'\x00' in position 0: invalid decimal Unicode string


Any ideas?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: data.bin
Type: application/macbinary
Size: 904 bytes
Desc: data.bin
URL: <http://mail.python.org/pipermail/python-list/attachments/20080129/1a2e670a/attachment.bin>


More information about the Python-list mailing list