Unicode and exception strings

Terry Carroll carroll at tjc.com
Wed Jan 14 17:48:46 EST 2004


On Wed, 14 Jan 2004 01:32:36 GMT, Terry Carroll <carroll at tjc.com> wrote:

>You can try to extract it as above, and then decode it with the codecs
>module, but if it's only the first byte, it won't decode correctly:
>
>>>> import codecs
>>>> d = codecs.getdecoder('utf-8')
>>>> x.args[0]
>u'\xf8'
>>>> d.decode(x.args[0])
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>AttributeError: 'builtin_function_or_method' object has no attribute
>'decode'
>>>>

Oops.  Copy-and-pasted the wrong line here.  Let's try that again:

>>> x = ValueError(u'\xf8')
>>> import codecs
>>> d = codecs.getdecoder('utf-8')
>>> d(x.args[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf8' in
position 0:
ordinal not in range(128)
>>>

*That's* the exception I was trying to show, not the AttributeError you
get when you use the decoder wrongly!




More information about the Python-list mailing list