UnicodeError: ASCII decoding error: ordinal not in range(128)

Brian Quinlan brian at sweetapp.com
Wed Jan 9 03:57:18 EST 2002


Hrvoje Nezic wrote:
> I get this error whan I try to use characters above 128.
> (I am using Python 2.2 on Windows 2000).
> 
> Python FAQ suggests the following to change
> the default encoding:
> 
> import sys
> sys.setdefaultencoding(encoding)

I can't believe the Python FAQ suggests that! You should stay away from
that routine (though I'll tell you how to use it later :-).

The real question is: what is the interpretation of characters above 128
in your application? Is the string data encoded using Latin-1, UTF-8,
mbcs, etc? You need to be able to answer this question to know how to
treat characters above 128.
 
> However, the module sys (at least at my machine) doesn't have
> setdefaultencoding function.
> 
> Is there some other way to fix that?

You shouldn't do this but....

>>> import sys
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('mbcs')
>>> str(u'\u00e9')
'\xe9'

Cheers,
Brian





More information about the Python-list mailing list