如何更改Python IDLE的默认字符编码? [英] How to change default character encoding for Python IDLE?

查看:285
本文介绍了如何更改Python IDLE的默认字符编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows上使用Python 3.6.当我使用标准Windows Shell( cmd.exe )运行脚本时, stdin/stdout 的默认文本编码为'utf-8'如Python 3.x所预期:

I'm using Python 3.6 on Windows. When I run a script using the standard Windows shell (cmd.exe), the default text encoding for stdin/stdout is 'utf-8' as expected in Python 3.x:

python -c "import sys; print(sys.stdout.encoding)"
utf-8

但是,在IDLE shell上使用相同的命令会得出不同的结果,这显然很烦人,特别是对于将IDLE用作第一步IDE的初学者.

However, the same command on the IDLE shell leads to a different result, which is clearly annoying, especially for beginner students using IDLE as a first step IDE

>>> import sys; print(sys.stdout.encoding)
cp1252

IDLE可能定义了 PseudoOutputFile PseudoInputFile 类来包装 stdout/stdin .这些类包括一个隐藏的 _encoding 属性,该属性可用于根据需要切换编码

It happens that IDLE defines PseudoOutputFile and PseudoInputFile classes to wrap stdout/stdin. These classes include a hidden _encoding attribute which can be used to switch encoding as needed

>>> sys.stdout._encoding = 'utf-8'
>>> print(sys.stdout.encoding)
utf-8

但是,每次启动脚本时都会取消此设置,因为IDLE在运行模块时会重新启动其外壳.是否有任何长期解决方案可更改 stdin/stdout 的IDLE默认编码?

But this setting is cancelled each time you launch a script, as IDLE relaunches its shell when running a module. Is there any long-term solution to change IDLE's default encoding for stdin/stdout ?

推荐答案

对于2.7、3.5,您显示的命令行对我来说是

For 2.7, 3.5, the command line you show responds, for me, with cp437 - the IBM PC or DOS encoding. Output to the Windows console is limited to a subset of Basic Multilingual Plane (BMP) Unicode characters.

对于3.6版本,Python对Windows控制台的处理方式得到了显着改进,以使用utf-8,并可能根据字体的可用性打印任何unicode字符.

For 3.6, Python's handling of the Windows console was drastically improved to use utf-8 and potentially print any unicode character, depending on font availability.

对于所有当前版本,IDLE也会为我报告cp1252(拉丁语1).由于尝试获取系统编码,因此我不知道为什么会有所不同.但这几乎没有什么区别,因为它是虚拟或伪造的值.对我来说,非拉丁字符不能用latin1编码是有欺骗性的,而所有BMP字符都可以用IDLE打印.所以我考虑过更换.

For all current versions, IDLE also reports, for me, cp1252 (Latin 1). Since there is an attempt to get the system encoding, I don't know why the difference. But it hardly makes any difference as it is a dummy or fake value. To me, it is deceptive in that non-latin1 chars cannot be encoded with latin1, whereas all BMP chars can be printed in IDLE. So I have thought about a replacement.

当(unicode)字符串写入sys.stdout(通常与print一起)时,字符串对象在用户进程中被腌制为字节,通过套接字(实现细节可能会发生变化)发送到IDLE进程,然后被取消腌制回到一个字符串对象.效果就好像是使用无损utf编码之一对字符串进行了编码和解码.UTF-32可能与酸洗最接近.

When (unicode) strings are written to sys.stdout (usually with print), the string object is pickled to bytes in the user process, sent through a socket (implementation detail subject to change) to the IDLE process, and unpickled back to a string object. The effect is as if the string was encoded and decoded with one of the non-lossy utf codings. UTF-32 might be the closest to what pickling does.

IDLE进程调用tkinter text.insert(index,string),它要求tk在小部件中插入字符串.但这仅适用于BMP字符.最终效果似乎是输出编码是ucs-2,尽管我相信tk在内部使用了截断的utf-8.

The IDLE process calls tkinter text.insert(index, string), which asks tk to insert the string in the widget. But that only works for BMP characters. The net effect is as if the output encoding were ucs-2, though I believe tk uses a truncated utf-8 internally.

类似地,您在shell或编辑器中输入的任何BMP字符在显示后都可以发送给用户进程stdin.

Similarly, any BMP character you enter in the shell or editor can be sent to the user process stdin after being displayed.

无论如何,更改pseudofile.encoding无效,这就是为什么

Anyway, changing pseudofile.encoding has no effect, which is why it was made read-only by this part of the patch for issue 9290

-        self.encoding = encoding
+        self._encoding = encoding
+
+    @property
+    def encoding(self):
+        return self._encoding

最初的下划线表示_encoding是私有(不是隐藏的)实现细节,用户应该忽略它.

The initial underscore means that _encoding is a private (not hidden) implementation detail that should be ignored by users.

这篇关于如何更改Python IDLE的默认字符编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆