SyntaxError: Non-ASCII character ‘\xe5’ in file

created at 06-25-2021 views: 3

error prompt:

This error is the mistake of beginners, and pay attention when writing code:

problem causes:

Python defaults to ASCII as an encoding method. If you contain Chinese (or other non-English language) in your own Python source code (even the notes), even if you write yourself Python source file with UTF- 8 format is saved, but in fact, this is still not

Solution:

The first line of the source code file is added: #coding:utf-8, this can be avoided,

It can also be changed to the first line:#-_-_ coding: utf-8 _-_-

Note: This statement must be added at the first line in the source code.

Several concepts must first figure it out: 

  • The default Python file is encoded by ASCII, adding # -- coding: utf-8 -- in the header - The specified file is UTF-8, then it is said that you can use Chinese or other words.
  • cn = u "Chinese", this prefix u represents "Chinese" is Unicode encoding, which means cn is not a string but a Unicode. 
  • When you use the print output, you will automatically convert according to the local language environment to String.
created at:06-25-2021
edited at: 06-25-2021: