[Tutor] need help with syntax
John Fouhy
john at fouhy.net
Wed Jan 11 03:16:00 CET 2006
On 11/01/06, bill nieuwendorp <slow67 at gmail.com> wrote:
Hi Bill,
Some comments ---
> >>> import struct
> >>> import string
> >>> f = file('binary_file','rb')
> >>> line = f.readline()
> >>> L = tuple(line)
You can do slicing (things like L[:4]) on strings as well as on lists
and tuples. So there is probably no need for you to convert to a
tuple --- just work with the lines directly.
> >>> s = L[:4]
> >>> s
> ('\x00', '\x00', '\x00', '\x06')
> >>> a = string.join( s , '')
You can also do:
a = ''.join(s)
and this is considered better python these days.
> 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
Sounds like you want string substitutions.. You can read about them
here: http://python.org/doc/2.4.2/lib/typesseq-strings.html
For instance:
>>> steps = 6
>>> ">%s" % steps
'>6'
HTH!
--
John.
More information about the Tutor
mailing list