Python 在英文版 Windows 系统下显示中文报错该怎么解决?

2017-03-31 00:23:41 +08:00
 cstome

代码:

'''

-- coding: utf-8 --

print('中文') '''

报错:

''' File "C:\Users\Cstome\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 characters in position 85-90: character maps to <undefined> '''

local 切换成简体中文就好了,但除此之外还有其他方法吗?

1828 次点击
所在节点    Python
1 条回复
billlee
2017-03-31 00:27:46 +08:00
The character encoding is platform-dependent. Under Windows, if the stream is interactive (that is, if its isatty() method returns True), the console codepage is used, otherwise the ANSI code page. Under other platforms, the locale encoding is used (see locale.getpreferredencoding()).

Under all platforms though, you can override this value by setting the PYTHONIOENCODING environment variable before starting Python.

所以,试试启动 python 前 chcp 65001

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/351624

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX