Displaying Unicode on the console (Windows)

Martin v. Löwis martin at v.loewis.de
Mon Apr 14 15:57:39 EDT 2003


paul.moore at atosorigin.com (Paul Moore) writes:

> To use a concrete example, I'd like to print the Euro symbol. A
> Unicode string for this is u'\20a0'
> 
> >>> unicodedata.name(u'\u20a0')
> 'EURO-CURRENCY SIGN'

As Irmen points out, U+20AC is the euro sign; U+20A0 predates the
European monetary union, and denotes the European Currency Unit (ECU).

> >>> print u'\u20a0'.encode("latin-15")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> LookupError: unknown encoding: latin-15

There is no encoding latin-15. You probably mean "latin-9", which is
more formally known as "iso-8859-15" (in ISO 8859, not all encodings
are Latin: some are Arabic, Hebrew, Cyrillic, and Greek).

> 
> >>> print u'\u20a0'.encode("latin-1")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> UnicodeError: Latin-1 encoding error: ordinal not in range(256)

Right. Latin-1 supports neither the ECU nor the EUR.

> 
> >>> print u'\u20a0'.encode("iso8859_15")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "C:\Python22\lib\encodings\iso8859_15.py", line 18, in encode
>     return codecs.charmap_encode(input,errors,encoding_map)
> UnicodeError: charmap encoding error: character maps to <undefined>

Right. Only the EUR is in Latin-9, not the ECU.

> >>> print u'\u20a0'.encode("cp1258")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "C:\Python22\lib\encodings\cp1258.py", line 18, in encode
>     return codecs.charmap_encode(input,errors,encoding_map)
> UnicodeError: charmap encoding error: character maps to <undefined>

Likewise.

> As I say, I *know* this is subtle. I'm happy to work out the details,
> once I can get a simple example working. But how do I get started? How
> do I get u'\u20a0' to display on my screen as a Euro character???

You won't ever get this. The best you can hope for is to get a symbol
which looks like a C on the top-left of an E.

> PS While I can live with having to know details of how the console is
> configured in order to get this working, ideally I'd like to know a
> way of getting anything I need out of Windows (even if it requires API
> calls...), so that I can write something generic which, when given a
> Unicode string, can display it on the console without needing any
> extra information from the user...

Microsoft has added API to support output of arbitrary Unicode
characters on the console, regardless of the code page
(WriteConsoleW). This only works if the console uses a Unicode font
(Lucida Sans Unicode).

Regards,
Martin




More information about the Python-list mailing list