strange behaviour of str()
Fredrik Lundh
fredrik at pythonware.com
Wed Aug 31 06:50:08 EDT 2005
Juho Vuori wrote:
> str(u'lää') raises UnicodeEncodeError
> Is this behaviour sane? Possibly, but not documented at all.
str() on a Unicode string attempts to convert the string to an 8-bit
string using Python's default encoding, which is ASCII. "ä" is not
an ASCII character.
if this problem appears in a 3rd party program, that program has
not been properly internationalized.
> Somehow you'd expect str() to never fail.
except for id() and type(), virtually all builtins can fail. If you want
to convert something to a string no matter what it contains, repr() is
a better choice. If you want to convert Unicode strings to a given
byte encoding, you have to use the encode method.
</F>
More information about the Python-list
mailing list