[英]Why I cannot save file with Chinese characters when using Python 2.7.11 IDLE?
I just downloaded the latest Python 2.7.11 64bit from its official website and installed it to my Windows 10. And I found that if the new IDLE file contains Chinese character, like 你好, then I cannot save the file.我刚刚从它的官方网站下载了最新的Python 2.7.11 64bit,并将其安装到我的Windows 10。我发现如果新的IDLE文件包含汉字,比如你好,那么我无法保存文件。 If I tried to save it for several times, then the new file crashed and disappeared.如果我多次尝试保存它,那么新文件就会崩溃并消失。
I also installed the latest python-3.5.1-amd64.exe, and it does not have this issue.我也安装了最新的python-3.5.1-amd64.exe,也没有这个问题。
How to solve it?如何解决?
More: A example code from wiki page, https://zh.wikipedia.org/wiki/%E9%B8%AD%E5%AD%90%E7%B1%BB%E5%9E%8B更多:来自wiki页面的示例代码, https://zh.wikipedia.org/wiki/%E9%B8%AD%E5%AD%90%E7%B1%BB%E5%9E%8B
If I past the code here, StackOverflow alays warn me: Body cannot contain "I just dow".如果我通过此处的代码,StackOverflow 会警告我:Body cannot contain "I just dow"。 Why?为什么?
Thanks!谢谢!
More: I find this config option, but it does not help at all.更多:我找到了这个配置选项,但它根本没有帮助。 IDLE -> Options -> Configure IDLE -> General -> Default Source Encoding: UTF-8 IDLE -> 选项 -> 配置 IDLE -> 常规 -> 默认源编码:UTF-8
More: By adding u
before the Chinese code, everything will be right, it is great way. More:在中文代码前加上u
,就万事大吉了,好办法。 Like below:像下面这样:
Without u
there, sometimes it will go with corrupted code.如果没有u
,有时它会伴随着损坏的代码。 Like below:像下面这样:
Python 2.x uses ASCII as default encoding, while Python 3.x uses UTF-8. Python 2.x 使用 ASCII 作为默认编码,而 Python 3.x 使用 UTF-8。 Just use:只需使用:
my_string.encode("utf-8")
to convert ascii to utf-8 (or change it to any other encoding you need)将 ascii 转换为 utf-8(或将其更改为您需要的任何其他编码)
You can also try to put this line on the first line of your code:您也可以尝试将此行放在代码的第一行:
# -*- coding: utf-8 -*-
Python 2 uses ASCII as its default encoding for its strings which cannot store Chinese characters. Python 2 使用 ASCII 作为其不能存储中文字符的字符串的默认编码。 On the other hand, Python 3 uses Unicode encoding for its strings by default which can store Chinese characters.另一方面,Python 3 的字符串默认使用 Unicode 编码,可以存储汉字。
But that doesn't mean Python 2 cannot use Unicode strings.但这并不意味着 Python 2 不能使用 Unicode 字符串。 You just have to encode your strings into Unicode.您只需将字符串编码为 Unicode。 Here's an example of converting your strings to Unicode strings.下面是将字符串转换为 Unicode 字符串的示例。
>>> plain_text = "Plain text"
>>> plain_text
'Plain text'
>>> utf8_text = unicode(plain_text, "utf-8")
>>> utf8_txt
u'Plain_text'
The prefix u
in the string, utf8_txt
, says that it is a Unicode string.字符串utf8_txt
中的前缀u
表示它是一个 Unicode 字符串。
You could also do this.你也可以这样做。
>>> print u"你好"
>>> 你好
You just have to prepend your string with u
to signify that it is a Unicode string.您只需要在您的字符串前加上u
来表示它是一个 Unicode 字符串。
When using Python 2 on Windows:在 Windows 上使用 Python 2 时:
For file with Unicode characters to be saved in IDLE, a line对于要保存在 IDLE 中的具有 Unicode 字符的文件,一行
# -*- coding: utf-8 -*-
has to be added in its beginning.必须在其开头添加。
And for Unicode characters to show correctly in console output in Windows, if running a script, saved in a file , in IDLE console or in Windows shell, strings have to be prepended with u
:为了使 Unicode 字符在 Windows 的控制台输出中正确显示,如果运行脚本,保存在文件中,在 IDLE 控制台或 Windows shell 中,字符串必须以u
开头:
print u"你好" print u"Привет"
But in interactive mode, I discovered no need for this with cyrillic.但是在交互模式下,我发现西里尔字母不需要这个。
even in python 3.7 I still experience the same issue, UTF-8 still does the trick即使在 python 3.7 中我仍然遇到同样的问题,UTF-8 仍然可以解决问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.