Python文件编码不可使用UTF16

    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编码

相关文章
相关标签/搜索