Python Encoding Problems, python Encoding

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.