SoFunction
Updated on 2024-11-13

Python Chinese encoding knowledge points

How to output "Hello, World!" in Python. There is no problem with English, but if you output the Chinese character "Hello, World" you may run into Chinese encoding problems.
Python files that do not specify an encoding will report an error during execution:

#!/usr/bin/python
print "Hello, World".

The output of the above program execution is:

File "", line 2
SyntaxError: Non-ASCII character '\xe4' in file  on line 2, but no encoding declared; see /peps/ for details

The default encoding format in Python is ASCII format, which cannot print Chinese characters correctly without modifying the encoding format, so it will report an error when reading Chinese.
The solution is to add # -*- coding: UTF-8 -*- or #coding=utf-8 to the beginning of the file.

Note: Don't put spaces on either side of the = sign in #coding=utf-8.

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
print "Hello, world.";

The output result is:

So if you are in the learning process, the code contains Chinese, you need to specify the encoding in the header.

Note: The source code file uses utf-8 encoding by default, so you can parse Chinese normally without specifying UTF-8 encoding.
Note: If you are using an editor, you also need to set the format in which py files are stored to UTF-8, otherwise you will get an error message similar to the one below:

SyntaxError: (unicode error) ‘utf-8' codec can't decode byte 0xc4 in position 0:
invalid continuation byte

Pycharm setup steps:

  • Go to file > Settings and search for encoding in the input box.
  • locate Editor > File encodings,commander-in-chief (military) IDE Encoding cap (a poem) Project Encoding set toutf-8。