no struct.pack for unicode strings?

Martin v. Loewis martin at
Fri Oct 18 09:20:25 CEST 2002

Radovan Garabik <spam at> 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.


More information about the Python-list mailing list