no struct.pack for unicode strings?

Martin v. Loewis martin at v.loewis.de
Fri Oct 18 03:20:25 EDT 2002


Radovan Garabik <spam at melkor.dnp.fmph.uniba.sk> writes:

> Why there is no format to pack/unpack unicode strings?
> Or am I missing something?

It's not entirely clear what struct.pack should do with a Unicode
object: UTF-8, UTF-16 (big or little endian, with or without BOM),
UTF-32 (big or little endian, with or without BOM), system encoding,
...

Hence, no packing is provided.

> My application needs to struct.pack unicode strings, to save them into a
> file which can be then read by a C extension module where I need to
> access characters of the string (as Py_UNICODE).

To save Unicode in a file, I recommend to encode them as UTF-8, and
use PyUnicode_DecodeUTF8 in your extension module to restore the
Unicode object.

Regards,
Martin




More information about the Python-list mailing list