[英]Python - 'charmap' codec can't encode character '\xe3'
When I type this in IDLE, it works: 当我在IDLE中输入此代码时,它可以正常工作:
>>> print('ã')
ã
But when I try to run the code (python main.py), it gives me this error: 但是当我尝试运行代码(python main.py)时,它给了我这个错误:
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: 这是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. 您似乎正在美国英语区域Windows控制台中运行,该控制台使用代码页437对其输出进行编码。 Code page 437 doesn't support the character ã
. 代码页437不支持字符ã
。
You can use a 3rd party library like win-unicode-console
or switch code pages. 您可以使用第三方库,例如win-unicode-console
或切换代码页。 Code page 1252 works: 代码页1252的工作原理:
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. 不幸的是,Windows控制台对UTF-8之类的编码的支持不佳,该编码支持整个Unicode字符范围,因此最好坚持使用UTF-8支持的IDE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.