Python supports 3 kinds of code declarations, commonly used to see the following two
1.# -*-coding:utf-8-*-
This is written to be compatible with Emacs's code declaration.
2. Short, but Emacs cannot be used # coding=utf-8
A little shorter, but Emacs can't be used
The reason to declare an unencrypted type is mainly the problem of Chinese error.
At the beginning of the Python file (typically the first or second line), use the encoding used to describe your Python source program file. By default, your program needs to write in ASCII code, but if you write Chinese in it, the Python interpreter will usually make an error, but if you add the file encoding you use, Python will automatically process the error again.
Here's what to note:
1.coding later use ":" or "=" can
2. However, ":" or "=" must have no spaces between coding. before I tried to have a space declaration failed, or not support Chinese. As for ":" or "=" after, there is no space on the so-called.
See: https://www.python.org/dev/peps/pep-0263/
Defining the Encoding
Python would default to ASCII as standard encoding if no other encoding hints is given. To define a source code encoding, a magic comment must is placed into the source files either as first or second l Ine in the file, such as: # coding=<encoding name> or (using formats recognized by popular editors) #!/u Sr/bin/python #-*-Coding: <encoding name>-*- or #!/usr/bin/python # vim:set fileencoding= <encoding name>: More precisely, the first or second line must match the regular expression "coding[:=]\s * ([-\w.] +)". The first group of this expression was then interpreted as encoding name. If the encoding is unknown to Python, an error is raised during compilation. There must not being any Python statement on the line that contains the encoding declaration.
Python file Encoding Description coding=utf-8