Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

26 views
Skip to first unread message

Hrvoje Nezic

unread,
Jan 9, 2002, 2:32:29 AM1/9/02
to
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)

However, the module sys (at least at my machine) doesn't have
setdefaultencoding function.

Is there some other way to fix that?

Regards,
Hrvoje Nezic


Brian Quinlan

unread,
Jan 9, 2002, 3:57:18 AM1/9/02
to
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


Oleg Broytmann

unread,
Jan 9, 2002, 6:36:30 AM1/9/02
to
On Wed, Jan 09, 2002 at 12:57:18AM -0800, Brian Quinlan wrote:
> 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 :-).

I wrote this entry in the FAQ, and I really meant it. But I said there,
in the FAQ, that it is valid in sitecustomize.py. Ok, this is valid *only*
in the sitecustomize.py.

Oleg.
--
Oleg Broytmann http://phd.pp.ru/ p...@phd.pp.ru
Programmers don't die, they just GOSUB without RETURN.

Brian Quinlan

unread,
Jan 9, 2002, 12:46:43 PM1/9/02
to
Oleg wrote:
> > 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 :-).
>
> I wrote this entry in the FAQ, and I really meant it. But I said
there,
> in the FAQ, that it is valid in sitecustomize.py. Ok, this is valid
*only*
> in the sitecustomize.py.

Fair enough.

Cheers,
Brian


0 new messages