packing (struct module)

dj trombley worldwin at liii.com
Fri Dec 17 01:57:17 EST 1999


> import struct
> 
> f = open( 'afile', 'r' )
> lines = f.readlines()
> packedLines = []
> 
> if len( lines ) > 0:
>     formatString = '1f' * lines[ 0 ].len()
> 
>     for l in lines:
>         packedLines.append( struct.pack( formatString, l ))
> 
> At this moment I would have a list of packed strings in packedLines.
> The problem is in the last line of code, since I cann't pass a list to
> the pack function.

There is a mechanism for applying a list of arguments to a function. It
is a builtin 
called apply().

For example:

	packedLines.append(apply(struct.pack, l))

apply() takes as its second argument a tuple, but will convert arbitrary
sequences to
tuples if it needs to.

-dj

Dave Trombley
<badzen at yifan.net>



More information about the Python-list mailing list