unable to print Unicode characters in Python 3

Benjamin Kaplan benjamin.kaplan at case.edu
Mon Jan 26 19:44:35 CET 2009


On Mon, Jan 26, 2009 at 1:16 PM, jefm <jef.mangelschots at gmail.com> wrote:

> Hi,
> while checking out Python 3, I read that all text strings are now
> natively Unicode.
> In the Python language reference (http://docs.python.org/3.0/reference/
> lexical_analysis.html<http://docs.python.org/3.0/reference/lexical_analysis.html>)
> I read that I can show Unicode character in
> several ways.
> "\uxxxx" supposedly allows me to specify the Unicode character by hex
> number and the format  "\N{name}" allows me to specify by Unicode
> name.
> Neither seem to work for me.
> What am I doing wrong ?
>
> Please see error output below where I am trying to show the EURO sign
> (http://www.fileformat.info/info/unicode/char/20ac/index.htm):
>
> Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> print('\u20ac')
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "c:\python30\lib\io.py", line 1491, in write
>    b = encoder.encode(s)
>  File "c:\python30\lib\encodings\cp437.py", line 19, in encode
>    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
> UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in
> position 0: character maps to <undefined>
> >>>
> >>> print ("\N{EURO SIGN}")
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "c:\python30\lib\io.py", line 1491, in write
>    b = encoder.encode(s)
>  File "c:\python30\lib\encodings\cp437.py", line 19, in encode
>    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
> UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in
> position 0: character maps to <undefined>
> --
>  <http://mail.python.org/mailman/listinfo/python-list>



The strings are represented internally as Unicode, but you can't print them
that way. When you call print(), python needs to turn your string into a
sequence of bytes that are then interpreted by the terminal (in your case
cmd.exe). On modern Unix-based systems (like Mac and Linux), the console
uses UTF-8. Since it uses a unicode-based encoding, everything prints fine.
Your Windows machine seems determined to use the old (c. 1980s) CP 437. Code
Page 437 doesn't have the Euro symbol, so python throws an error. Try using
"\N{EURO SIGN}".encode("cp1252"). If your console still can't handle it,
you'll need to change its encoding.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090126/8b98787e/attachment.html>


More information about the Python-list mailing list