A machine is not a perpetual motion machine. It makes mistakes and stops to rest. A relative piece of code is not always reliable and can run with many unexpected errors, so today we’ll take a look at Python coding errors.

There are two common reasons for runtime display coding errors in Python:

The encoding and decoding methods are inconsistent

When writing Python, you will get an error message when running a script with Chinese output or comments:

SyntaxError:Non-ASCIIcharacter’\xe5’infile*******

“Why”

The Python interpreter’s default encoding file is ASCII, and your Python file uses non-English characters such as Chinese.

[Solution]

At the beginning of the Python source file, add the following line:

#coding=UTF-8

Note: There are no Spaces on the left and right sides of the equal sign

Supplement:

The encoding method of the.py file is determined by the editor. In Chinese environments, the editor’s encoding method is usually UTF-8. When decoding, the Python interpreter must decode the file in the same way as the source file.

Some characters cannot be decoded

The encoding and decoding methods are consistent, but some characters cannot be decoded.

TXT file to read Chinese, often appear: ‘GBK’ codeccan tdecodebytesinposition31023: illegalmultibytesequence.

In this case, the text contains characters not found in GBK encoding.

The best solution:

Use GB18030 encoding, because gb18030 encoding contains GBK, supports more characters, and adds ‘ignore’ to ignore characters that cannot be read.

For example;

supplement

Python2 file paths do not have Chinese, which can cause some strange bugs, but python3 does not.

Python learning materials are free of charge