Python nuube needs Unicode help

Peter Otten __peter__ at web.de
Fri Jan 12 03:27:06 EST 2007


gheissenberger at gmail.com wrote:

> HELP!
> Guy who was here before me wrote a script to parse files in Python.
> 
> Includes line:
> print u

According to your other posts 'u' seems to be an instance of a custom
Utterance class with a __str__() method that accidentally returns unicode.
Try changing the print statement to

print unicode(u)

If you're lucky, it works. Otherwise we need a piece of the actual code. To
give you an idea what a self-contained demonstration of your problem might
look like:

>>> class Utterance(object):
...     def __str__(self): return u"äöü"
...
>>> u = Utterance()
>>> print u
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2:
ordinal not in range(128)
>>> print unicode(u)
äöü

Peter



More information about the Python-list mailing list