Why can't I save a Chinese character file when using Python 2.7.11 IDLE?

I just downloaded the latest version of Python 2.7.11 from its official site and installed it on my Windows 10. And I found that if the new IDLE file contains a Chinese character, for example 你好, then I cannot save the file. If I tried to save it several times, the new file crashed and disappeared.

I also installed the latest python-3.5.1-amd64.exe and it does not have this problem.

How to solve it?

More: Sample code from the wiki page, https://zh.wikipedia.org/wiki/%E9%B8%AD%E5%AD%90%E7%B1%BB%E5%9E%8B

If I skipped the code here, StackOverflow warns me: Body cannot contain "I'm just a dow." Why?

Thanks!

enter image description here

More: I find this configuration option, but that doesn't help at all. IDLE -> Options -> IDLE Settings -> General -> Default Source Code Encoding: UTF-8

More: By adding u in front of the Chinese code, everything will be correct, this is a great way. As below: enter image description here

Without u there, sometimes it will go with corrupted code. As below: enter image description here

+5
source share
3 answers

Python 2.x uses ASCII as the default encoding, while Python 3.x uses UTF-8. Just use:
my_string.encode("utf-8")
convert ascii to utf-8 (or change it to any other encoding you need)

You can also try putting this line in the first line of your code:

 # -*- coding: utf-8 -*- 
+2
source

Python 2 uses ASCII as the default encoding for its strings that cannot store Chinese characters. Python 3, on the other hand, uses Unicode for its strings by default, which can store Chinese characters.

But this does not mean that Python 2 cannot use Unicode strings. You just need to encode your lines in Unicode. Here is an example of converting strings to Unicode strings.

 >>> plain_text = "Plain text" >>> plain_text 'Plain text' >>> utf8_text = unicode(plain_text, "utf-8") >>> utf8_txt u'Plain_text' 

The u prefix in the string, utf8_txt , says it is a Unicode string.

You can also do this.

 >>> print u"你好" >>> 你好 

You just need to add the string with u to designate it as a Unicode string.

+2
source

When using Python 2 on Windows:

  • For a file with Unicode characters that must be stored in IDLE, the line

     # -*- coding: utf-8 -*- 

    should be added at the beginning.

  • And for Unicode characters, to correctly display on the console output in Windows when running a script, saved in a file , in the IDLE console or in the Windows shell, the lines should be added using u :

     print u"你好" print u"" 

    But in interactive mode, I did not find the need for this with the Cyrillic alphabet.

0
source

Source: https://habr.com/ru/post/1239355/


All Articles