Encoding of String

Martin v. Loewis martin at v.loewis.de
Mon Nov 4 12:09:20 EST 2002


Thomas Guettler <zopestoller at thomas-guettler.de> writes:

> Is there a way to get the encoding of a string?

No. The absence of a reliable method to detect the encoding of some
arbitrary sequence of bytes is the very reason to introduce explicit
declarations of encodings into various protocols and format
(e.g. MIME, XML, etc). In the end, this problem is also one of the
reasons for Unicode to exist, and the Unicode type in Python as a
separate data type: If you have a Python Unicode object, you know how
to interpret the elements of the sequence. For an arbitrary string,
you don't.

That said, there are various heuristics you can apply; search this
group for earlier discussions of the matter.

Regards,
Martin




More information about the Python-list mailing list