1. The complete Python source file should use a single encoding. Embedding of differently encoded data is not allowed and will result in a decoding error during compilation of the Python source code.
Python源文件應該使用單一編碼,嵌入不一樣編碼的數據是不容許的(我的猜想:好比單一文件裏一部分使用GBK,一部分使用BIG碼是不行的),會致使解碼錯誤。
Any encoding which allows processing the first two lines in the way indicated above is allowed as source code encoding, this includes ASCII compatible encodings as well as certain multi-byte encodings such as Shift_JIS. It does not include encodings which use two or more bytes for all characters like e.g. UTF-16. The reason for this is to keep the encoding detection algorithm in the tokenizer simple.
任何容許處理頭兩行的編碼能夠做爲源代碼編碼的格式,這包括ASCII兼容編碼以及某些多字節編碼,好比SHIFT_JIS。它不包括爲全部字符都是有雙字節或者更多字節的編碼,好比UTF-16(注:也就是一般說的Unicode,但SHIFT_JIS也好,GBK也好,由於兼容ASCII編碼,因此均可以在Python源文件裏使用)。這麼作的理由是,可使用簡單的字符在編碼探測算法裏。
摘自這裏:python
http://legacy.python.org/dev/peps/pep-0263/算法
# -*- coding: UTF-8 –*-
#coding=utf-8
#coding:utf-8
#coding=gbk編程
結論:可使用utf-8,若是涉及跨平臺的時候不要帶BOM,也能夠採用GBK,但就是不能使用utf16。可是有意思的是,我使用PyScripter做爲IDE編程,源代碼裏帶中文註釋,把Python文件轉成UTF16-LE後編程和運行一點問題都沒有,還有部分優點,好比能正確顯示200 µs,utf8下就有亂碼。可是發佈的時候,可不要這樣。this
這篇文章不錯:
http://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/
http://www.jb51.net/article/26543.htm編碼