strange behaviour of str()
schmitt at num.uni-sb.de
Wed Aug 31 12:58:32 CEST 2005
> I'm wondering about the following behaviour of str() with strings
> containing non-ASCII characters:
> str(u'foo') returns 'foo' as expected.
> str('lää') returns 'lää' as expected.
> str(u'lää') raises UnicodeEncodeError
This does not work, because you need an encoder to convert
unicode to str. str() does not know a priori which encoder
to use. There are many ways to encode a unicode string
to a classic byte-stream based string.
you have to procede as follows:
>>> print s.encode("latin-1")
try "utf-8" and "utf-16" instead of "latin-1"
More information about the Python-list