Unicode
Peter Otten
__peter__ at web.de
Sun Sep 17 15:27:11 EDT 2017
leam hall wrote:
> Doesn't seem to work. The failing code takes the strings as is from the
> database. it will occasionally fail when a name comes up that uses
> a non-ascii character.
Your problem in nuce: the Python 2 __str__() method must not return unicode.
>>> class Character:
... def __str__(self): return u"Brösel"
...
>>> print(Character())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position
2: ordinal not in range(128)
While you may define a __unicode__ method it has to be called explicitly:
>>> class Character:
... def __unicode__(self): return u"Brösel"
...
>>> print(Character())
<__main__.Character instance at 0x7fc10020f5a8>
>>> print(unicode(Character()))
Brösel
Another alternative is to convert explicitly, to some encoding, and hope it
works in the actual environment:
>>> class Character:
... def __unicode__(self): return u"Brösel"
... def __str__(self): return unicode(self).encode("utf-8")
...
>>> print(Character())
Brösel
The more you think about it the more attractive a switch to Python 3 will
appear.
More information about the Python-list
mailing list