Python basic finishing - a preliminary study of Chinese coding

   When writing English and Pinyin, the python program can run normally, but when writing Chinese characters, if the editor used is not coded, it may report an error. In order to better solve this problem, I refer to the rookie tutorial for a summary:

In the window environment:

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

#!/usr/bin/python

(The first line of this language is to point out what executable program you want your code in this file to use to run it)

#!/usr/bin/python tells the operating system to call the python interpreter under /usr/bin when executing this script.
#!/usr/bin/env python This usage is to prevent operating system users from not installing python in the default /usr/bin path . When the system sees this line, it will first look for the python installation path in the env settings, and then call the interpreter program under the corresponding path to complete the operation. This way of writing will go to the environment settings to find the python directory, this way of writing is recommended)

 print "Hello, world" ; 

The output of the above program execution is:

 File"test.py", line 2SyntaxError: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 just add at the beginning of the file

  • # -*- coding: UTF-8 -*-
  • Or #coding=utf-8 will do

If there is no declaration of the encoding type of this file, python will process it in ASCII encoding by default ; if you do not declare the encoding, but the file contains non-ASCII encoded characters, the python file to be parsed by the python parser will naturally report an error. .

In Linux environment:

The only difference in the Linux environment is the "\" difference, you need to replace the "/" in Windows with the "\" character.

 

Guess you like

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