简体   繁体   中英

Python - 'charmap' codec can't encode character '\xe3'

When I type this in IDLE, it works:

>>> print('ã')
ã

But when I try to run the code (python main.py), it gives me this error:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    print('\xe3')
  File "C:\Users\Gustavo\AppData\Local\Programs\Python\Python35\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 '\xe3' in position 0: character maps to <undefined>

Here's the code of main.py:

print('ã')

Please help me.

It looks like you are running in a US English-locale Windows console, which uses code page 437 for encoding its output. Code page 437 doesn't support the character ã .

You can use a 3rd party library like win-unicode-console or switch code pages. Code page 1252 works:

C:\>chcp 1252
C:\>python main.py
ã

Unfortunately the windows console has poor support for encodings like UTF-8, which support the entire range of Unicode characters, so it's better to stick to an IDE with UTF-8 support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM