python文件编码说明 coding

权威来源:http://www.python.org/dev/peps/pep-0263/
Python源文件的头部声明(声明在文件的第一行或第二行)

# coding=utf-8

# coding:utf-8

# -*- coding:utf-8 -*-

要符合正则规范"coding[:=]s*([-w.]+)"  注意 :或=前后没有空格

Defining the Encoding

    Python will default to ASCII as standard encoding if no other
    encoding hints are given.

    To define a source code encoding, a magic comment must
    be placed into the source files either as first or second
    line in the file, such as:

          # coding=<encoding name>

    or (using formats recognized by popular editors)

          #!/usr/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 is then interpreted as encoding name. If the encoding
    is unknown to Python, an error is raised during compilation. There
    must not be any Python statement on the line that contains the
    encoding declaration.

PEP 的全名是 Python Enhancement Proposals, "Python 增強建議書" ,它是用來規範與定義 Python 的各種加強與延伸功能的技術規格。

每個 PEP 都有一個編號而且是唯一的,這個編號一旦給定了就不會再改變,例如,PEP 3000 就是用來定義 Python 3.0 的相關技術規格,而 PEP 333 則是 Python 的 Web 應用程式介面 WSGI (Web Server Gateway Interface 1.0) 的規範

關於 PEP 本身的相關規範是定義在 PEP 1,而所有的 PEP 列表則是在這裡

Read more: http://www.arthurtoday.com/2009/11/python-pep.html#ixzz46oH9oBzV

学习:http://blog.csdn.net/orangleliu/article/details/8755461

原文地址:https://www.cnblogs.com/dennysong/p/5430502.html