The default encoding for strings in Python code is the same as the encoding of the code file itself
The role of Decode is to convert other encoded strings into Unicode encoding
The role of encode is to convert a Unicode encoding into another encoded string
>>> s= "Chinese"
>>> s
' Xd6xd0xcexc4 '
>>> S.decode ("GBK")
U ' u4e2du6587 '
>>> print S.decode ("GBK")
Chinese
>>> Print S
Chinese
>>> S.decode ("GBK"). Encode ("GBK")
' Xd6xd0xcexc4 '
>>> print S.decode ("GBK"). Encode ("GBK")
Chinese
>>>
>>> a= ' Chinese '
>>> A
' XD6XD0XB9XFAXC8XCB '
>>> B = Unicode (A, ' GBK ')
>>> b
U ' U4e2du56fdu4eba '
>>> a.find (' Middle ')
0
>>> a.find (' People ')
4
>>> b.find (' People ' decode (' GBK '))
2
>>> Print a
Chinese
>>> Print B
Chinese
Here the Find function found that the results should be very well understood. Find in B must be decode, otherwise there will be an error. As for why print is Chinese characters, I have not yet studied it out. Please tell the master.
>>> b.encode (' GB18030 ')
' XD6XD0XB9XFAXC8XCB '
>>> b.encode (' cp936 ')
' XD6XD0XB9XFAXC8XCB '
>>> b.encode (' GBK ')
' XD6XD0XB9XFAXC8XCB '
>>> b.encode (' Utf-8 ')
' Xe4xb8xadxe5x9bxbdxe4xbaxba '
Description GB18030, GBK, gb2312 and cp936 can be encoded in Chinese, and the results are consistent, utf-8 can also, but the encoding is not the same, so the results are different. They are all consistent in principle.
>>> type (a)
<type ' str ' >
>>> type (b)
<type ' Unicode ' >
>>> type (b.encode (' Utf-8 '))
<type ' str ' >
Shows that Python has two encodings for strings, one is the normal way, and the other is Unicode. Note that Utf-8 is also considered a common coding method.
Edit conversions are provided below
Chinese characters are converted into HTML entity characters.
Echo mb_convert_encoding ("re-play Once", "html-entities", "gb2312");
Encoding Conversion
s = "Chinese"
S1 = u "Chinese"
Unicode-> GBK
s1.encode ("GBK")
Unicode-> utf-8
s1.encode ("UTF-8")
GBK-> Unicode
unicode (S, "GBK")
or
s.decode ("GBK")