Re: strange behaviour of str()
- From: "Fredrik Lundh" <fredrik@xxxxxxxxxxxxxx>
- Date: Wed, 31 Aug 2005 12:50:08 +0200
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>
.
- References:
- strange behaviour of str()
- From: Juho Vuori
- strange behaviour of str()
- Prev by Date: Re: Python doc problems example: gzip module
- Next by Date: Re: Bicycle Repair Man usability
- Previous by thread: strange behaviour of str()
- Next by thread: strange behaviour of str()
- Index(es):
Relevant Pages
|
|