1. Whether py2 or PY3, the conversion of character encodings between the characters, such as GBK to Utf-8, is required through Unicode transit
,
2. When converting non-Unicode to Unicode, it is necessary to tell what type, as originally utf-8, should be converted to Unicode as follows:
string1 = "Hello"
Suppose string1 was originally utf-8 and needed to be converted to Unicode,
The conversion process is: String1.decode ("Utf-8")
3. If you need to convert the original utf-8 to GBK, the process should convert the string1 to Unicode first and then to GBK
String1.decode ("Utf-8")
String1.encode ("GBK") is specified as the converted character encoding
Python3, in encode, not only encodes the character of the string change, but also changes it into binary format.
Python2 The interpreter in the default ASCII code to interpret the file, Python3 in the utf-8 to explain the file, but you can by the beginning of the file with #-*-Coding:utf-8-*-to tell the interpreter to decode Utf-8
In Python3, the data type within the file is Unicode encoded, such as the character type of the string is Unicode
1 ImportSYS2 Print(Sys.getdefaultencoding ()) #打印系统默认编码3 4s ="Hangzhou" #the string type in the file in Python 3 defaults to Unicode5 Print(S,type (s))6 Print(s)7s = S.encode ("GBK")#encode Unicode into GBK8 Print(S,"=========")9S_to_unicode = S.decode ("GBK")#converting the encoding from GBK to Unicode, this awareness is to inform s that the encoding needed to be GBK is converted to UnicodeTen Print(S_to_unicode) One AUnicode_to_utf8 = S_to_unicode.encode ("Utf-8")#convert the original Unicode to Utf-8 - Print(Unicode_to_utf8) -utf8_to_gb2312 = Unicode_to_utf8.decode ("Utf-8"). Encode ("gb2312")#Convert the original utf-8 to gb2312, the middle needs to be converted to Unicode by first decode, and then convert Unicode through encode to gb2312 the Print(utf8_to_gb2312)
1. In Python2 the default encoding is ASCII, the default is Unicode in Python3
2.unicode is divided into utf-32 (4 bytes), utf-16 (accounting for two bytes), Utf-8 (1-4 bytes), so utf-16 is now the most commonly used Unicode version, but in the file is still utf-8, because the UTF8 save space
3. Encode in Py3, while transcoding will also change the string to bytes type, decode decoding will also turn bytes back to string
4. ASCII occupies a byte, gbk,gb2312 occupies two bytes, UTF-8 encoding is variable-length encoding, usually Chinese characters accounted for three bytes, extended B area after the Chinese characters accounted for four bytes.
Reference Link: http://www.cnblogs.com/yuanchenqi/articles/5956943.html
Python---character encoding