Tom Xue Tom Xue - 5 years ago 363
Python Question

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. 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.

How to solve it?

A example code from wiki page,

If I past the code here, StackOverflow alays warn me: Body cannot contain "I just dow". Why?


enter image description here

I find this config option, but it does not help at all.
IDLE -> Options -> Configure IDLE -> General -> Default Source Encoding: UTF-8

By adding

before the Chinese code, everything will be right, it is great way. Like below:
enter image description here

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

Answer Source

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

You can also try to put this line on the first line of your code:

# -*- coding: utf-8 -*-
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download