ASCII encoding error: ordinal not in range(128)

Fredrik Lundh fredrik at pythonware.com
Tue Mar 20 17:45:10 EST 2001


Jose Isaias Cabrera wrote:

> UnicodeError: ASCII encoding error: ordinal not in range(128)

translated to english, this overly cute error message attempts
to say something like "cannot convert this Unicode string to
ASCII as it contains non-ASCII characters"

> I've search on python sites for this, and though there is
> something about Unicode, there is no python example code
> to use.
>
> Does anyone have any idea how to fix this?

summary: someone's handing you unicode strings (probably
the COM interface), and you're trying to use them as 8-bit
strings (probably by passing them to a method that expects
8-bit data) without specifying an encoding.

(if you want me to be more precise, you have to show us
more code).

to solve this, you have to add an explicit conversion to the
right place.  e.g:

    s = u.encode("latin-1")

where "u" is a unicode string and "s" an 8-bit ISO Latin string.

(if you don't do this, Python will convert the string anyway when
you pass it to an 8-bit function.  the default conversion assumes
ASCII, which is why you get that exception)

Hope this helps!

Cheers /F





More information about the Python-list mailing list