I. Source file code (ENCODING:UTF-8)
1.
Python 2.x by default by ASCII encoding read source files, the source code in the ASCII can not be represented by the character "", so error (3.x version does not error).
2.
The source code is added with Encoding:utf-8, which allows non-ASCII characters to appear in the original file, but is also transcoded when output in Windows DOS (GBK is converted here).
3.
Both the encoding of the source file and the output code of the DOS are GBK, and the content is consistent.
Ditto.
Second, string encoding
1. The default encoding for string storage:
Python 2.7
Python 3.3.3
2. Unicode object and normal string str
For S, press Utf-8 to decode to the Unicode object, and then encode the Unicode object by GBK.
A direct error in GBK encoding of the STR string is because the default encoding of the string (Python 2.7) is ASCII and the default is S.decode (' ASCII '). Encode (' GBK '), so an error occurs.
Set the string default encoding to Utf-8, no more error.
In Python we use decode () and encode () to decode and encode
In Python, the Unicode type is used as the underlying type of the encoding. That
Decode encode
STR--------->unicode--------->str
For Python 2.x, Python 3.x has changed, one version of a language.
Third, basestring, str, Unicode
3.1 STR and Unicode are subclasses of basestring:
3.2 Create a Unicode object using str:
See also: http://www.cnblogs.com/evening/archive/2012/04/19/2457440.html
[Python Learning] Encoding