converting to string

Peter Hansen peter at engcorp.com
Wed Aug 4 08:01:05 EDT 2004


Gandalf wrote:

> Ajay Brar wrote:
>> how do i go about converting a Python object or tuple to a string.
> 
> For special cases you can find a more efficient solution.For example, 
> for tuples of integers,
> you can write a C extension that allocates a big string and copies every 
> int object into 4 bytes
> in the string. That would be more efficient.

A C extension?  You mean like this? :

 >>> s = (1, 55, 109, 2000, 65535)
 >>> import struct
 >>> struct.pack('%si' % len(s), *s)
'\x01\x00\x00\x007\x00\x00\x00m\x00\x00\x00\xd0\x07\x00\x00\xff\xff\x00\x00'

<wink>

-Peter



More information about the Python-list mailing list