Python Encoding Problems, python Encoding
SCII encoding is one byte, while Unicode encoding (Chinese characters) is usually two bytes. One byte, 8 bits)If Unicode encoding is used, English letters occupy 2 bytes, resulting in a waste of space. As a result, utf8 variable encoding occurs, which is in utf8 encoding. The English letter encoding occupies one byte, and the Chinese character is usually three bytes. If the transmitted text and Chinese characters are mixed with English, utf8 can save space.If Unicode encoding is used in computer memory, it must be saved as a file or transferred to utf8 encoding.When browsing the Web page, the server will replace the Unicode content with utf8 and transmit it to the browser.1. Python provides the ord () and chr () functions to convert letters and corresponding ASCII codes to each other.1 >>> ord ('A') 2 653 >>> chr (65) 4 'A' View Code
2. Python later added support for Unicode. The Unicode string is represented by U'... ', for example:
1 >>> print u'chinese' 2 Chinese 3 >>> u'chinese' 4 U' \ u4e2d \ u6587 '5 >>> print U' \ u4e2d \ u6587 '6 the content after Chinese 7 u'' is in hexadecimal Unicode encoding. Unicode is a Chinese character in 2 bytes, u'chinese' occupies 4 bytes of 8 >>> u'chinese '. encode ('utf-8') 9' \ xe4 \ xb8 \ xad \ xe6 \ x96 \ x87 '10 is converted to utf8 encoding. You can see that a Chinese character occupies 3 bytes of View Code.
3 In turn, convert the UTF-8 encoded string 'xxx' to the Unicode string u 'xxx' using the decode ('utf-8') Method
1 >>> print '\ xe4 \ xb8 \ xad \ xe6 \ x96 \ x87'. decode ('utf-8') 2 Chinese View Code