[Tutor] need help with syntax

bill nieuwendorp slow67 at gmail.com
Wed Jan 11 02:25:28 CET 2006


hello all I am new to python and this list has been helpfull so far

I am trying to convert binary file to ascii

here is the format spec

steps = int 4
value = int 4
time = float 4 * steps

so in the python terminal terminal i convert it like this


>>> import struct
>>> import string
>>> f = file('binary_file','rb')
>>> line = f.readline()
>>> L = tuple(line)

then to get the steps value i do

>>> s = L[:4]
>>> s
('\x00', '\x00', '\x00', '\x06')
>>> a = string.join( s , '')
>>> steps = struct.unpack (">l" , a)
>>> steps
(6,)

and for value

>>> v = L[4:8]
>>> v
('\x00', '\x00', '\x00', '\x08')
>>> b = string.join( v , '')
>>> value = struct.unpack (">l" , b)
>>> value
(8,)

for the time value "this is where I need help"

I know steps = 6 so

>>> 4*6 + 8
>>> 32
>>> t = L[8:32]
>>> t
('\x00', '\x00', '\x00', '\x00', '=', '\x08', '\x88', '\x89', '=', '\x88', '\x88
', '\x89', '=', '\xcc', '\xcc', '\xcd', '>', '\x08', '\x88', '\x89', '>', '*', '
\xaa', '\xab')
>>> c = string.join(t , '')
>>> time = struct.unpack (">6f", c)
>>> time
(0.0, 0.033333335071802139, 0.066666670143604279, 0.10000000149011612, 0.1333333
4028720856, 0.1666666716337204)

so I was happy to figure this much out

what I need is a way to do somthing like this

time = struct.unpack(">steps",c)

but this does not work because steps is not an integer

any help would be great thanks


More information about the Tutor mailing list