python Chinese encoding

Python Chinese encoding

Python outputs "Hello, World!", there is no problem in English, but if you output the Chinese characters "Hello, World", you may encounter Chinese encoding problems.

If no encoding is specified in the Python file, an error will occur during execution:



File"test.py", line 2 SyntaxError:Non-ASCII character '\xe4'in file test.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details



The default encoding format in Python is ASCII format. If the encoding format is not modified, Chinese characters cannot be printed correctly, so an error will be reported when reading Chinese.

The solution is to add  # -*- coding: UTF-8 -*-  or  #coding=utf-8 at the beginning of the file  .



Pycharm setup steps:

  • Go to  file > Settings and search for encoding in the input box  .
  • Find  Editor > File encodings , set  IDE Encoding  and  Project Encoding  to utf-8.

File "test.py", line 2 SyntaxError : Non-ASCII character '\xe4'in file test.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325517944&siteId=291194637