Can Python write foreign characters to the console?

"Martin v. Löwis" martin at v.loewis.de
Sun Dec 18 13:41:24 EST 2005


Michel Claveau wrote:
> Hi!
> 
> I have a problem, under win-XP, with this code :
> 
> # -*- coding: cp-1252 -*-
> 
> import sys
> print u"Martin v. Löwis"
> print "€ for Noël"
> print u"€ for Noël"
> sys.exit()
> 
> ==> "Python a provoqué une erreur"
> 
> 
> Am I the only one to have that?

No. I get multiple errors with that code:

1. cp-1252 isn't defined. I had to use either cp1252,
   or windows-1252.
2. If I correct that, I still get

Martin v. Löwis
Ç for NoÙl
Traceback (most recent call last):
  File "xxx.py", line 6, in ?
    print u"Ç for NoÙl"
  File "C:\Python24\lib\encodings\cp850.py", line 18, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u20ac' in
position 0: character maps to <undefined>

This is not surprising: the first print statement works fine.
The second print statement interprets the CP 1252 in the terminal's
code page, which gives bogus results.
The third command tries the right thing: converting the Unicode
string to the terminal's encoding, CP 850. Now, while the Umlaut (ö)
is supported in CP 850, the EURO SIGN is not, hence the UnicodeError.

Regards,
Martin

P.S. I never get "Python a provoqué une erreur". Not sure where
this message comes from.



More information about the Python-list mailing list