forcing exceptions

Robert Kern robert.kern at gmail.com
Fri Mar 3 15:52:45 EST 2006


Nikola Skoric wrote:

> 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.

Presumably, self.sect[1] is not a unicode string, but a regular string. The
utf-8 encoder only takes unicode strings, so it tries to convert the input into
a unicode string first. ASCII is the default encoding when no encoding is specified.

Standard practice with unicode is to only do conversions at the boundaries
between your code and interfaces that really need bytes (files, the console, the
network, etc.). Convert to unicode strings as soon you get the data and only
convert to regular strings when you have to. Everything *inside* your code
should be unicode strings, and you shouldn't try to pass around encoded regular
strings if at all possible, although pure ASCII strings generally work because
it is the default encoding.

So double-check self.sect[1] and figure out why it isn't a unicode string.

-- 
Robert Kern
robert.kern at gmail.com

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list