Ordinal not in range(128)

Lexy Zhitenev zhitenev at cs.vsu.ru
Mon Mar 3 08:04:50 CET 2003

"Fabien HENON" <fabien.henon at caramail.com> wrote in message:
news:3e614c68$0$227$626a54ce at news.free.fr...
> UnicodeError: ASCII encoding error: ordinal not in range(128)

Certainly, this is a FAQ. I've had the same problem.

In case you don't wanna look into it, just modify your

# Set the string encoding used by the Unicode implementation.  The
# default is 'ascii', but if you're willing to experiment, you can
# change this.

encoding = "ascii" # Default value set by _PyUnicode_Init()

if 0:
    # Enable to support locale aware default string encodings.
    import locale
    loc = locale.getdefaultlocale()
    if loc[1]:
        encoding = loc[1]

if 0:
    # Enable to switch off string to Unicode coercion and implicit
    # Unicode to string conversion.
    encoding = "undefined"

if encoding != "ascii":
    # On Non-Unicode builds this will raise an AttributeError...
    sys.setdefaultencoding(encoding) # Needs Python Unicode build !

So, if you need some specific encoding, change one of the if 0: to if 1: or
set the encoding in any way you like.
Note that this will be the default encoding. To specify another one, you
still have to use S = U.encode('any_encoding')


More information about the Python-list mailing list