Unicode, COM, Word Problem

Fredrik Lundh fredrik at effbot.org
Sat Jan 20 05:17:44 EST 2001


Kirby James wrote:
> However when I try to print sText (or write it to a file
> output.write(sText)) I get an exception
>     UnicodeError: ASCII encoding error: ordinal not in range(128)

figure out what encoding your output device is using,
and use the "encode" method, e.g:

    output.write(sText.encode("iso-latin-1"))

(if you don't, Python assumes pure ASCII, and chokes
if the string contain anything else).

:::

the locale.getdefaultlocale() function can sometimes be
useful to figure out what encoding to use:

    >>> lang, enc = locale.getdefaultlocale()
    >>> print enc
    'cp1252'

Cheers /F





More information about the Python-list mailing list