forcing exceptions

Peter Otten __peter__ at web.de
Fri Mar 3 15:57:44 EST 2006


Nikola Skoric wrote:

> print self.sect[1].encode('utf-8')
> 
> Which results in:
> 
> Traceback (most recent call last):
> File "AIDbot2.py", line 238, in ?
> bot.checkNominations()
> File "AIDbot2.py", line 201, in checkNominations
> if sect.parseSect() == 1:
> File "AIDbot2.py", line 96, in parseSect
> print self.sect[1].encode('utf-8')
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 15:
> ordinal
> not in range(128)
> 
> Now, who can it complain about 'ascii' when I said loud and clear I want
> it to encode the string to 'utf-8'??? Damn unicode.

Trying to encode a str s results in something like

unicode(s, "ascii").encode("utf-8")

where the first step, i. e. the conversion to unicode fails for non-ascii
strings. The solution is to make that conversion explicit and provide the
proper encoding. Assuming the initial string is in latin1:

unicode(self.sect[1], "latin1").encode("utf-8")

Of course, if you can ensure that self.sect[1] is a unicode instance earlier
in your code, that would be preferable.

Peter



More information about the Python-list mailing list