Read the notes on how Python3 is solving tricky character coding problems

Source: Internet
Author: User

The coding for Python is noteworthy in that different versions are encoded differently. Python3 default is Utf-8 and Python2 is ASCLL, so the different versions of Unicodeencodeerror and unicodedecodeerror processing is not the same! So the understanding and use of decode () and encode () is not the same! But the principle of using two functions is the same! Decode () is the decoding of bytes into characters, encode () is the opposite! It is worth noting that bytes are convenient for storage and network transmission, while characters are used for display.

For Python3 's Coding summary: The text characters are all denoted by the STR type, and STR can represent all the characters in the Unicode character set, while the binary byte data is represented by a new data type, bytes. This is the object represented by the byte in front of the character quotation mark. But the limitation here is that it is not tender to speak Chinese.

A clear Picture:

It's time for the Python2 to play:

Python2 default is the byte represented by ASCLL,STR and Unicode represents a character. We want to save the Unicode symbol to a file or transfer it to the network and it needs to be encoded to be converted to the STR type, so Python provides the Encode method, which translates from Unicode to Str. The relationship between the two transitions is as follows:

The problem of attention

File reads and writes:

Write: Determine what type will be written, if it is a byte, write directly, if it is a character then it will first call the Encode method to convert the Unicode string into a binary form of the STR type, only to save to the file, and the Encode method will use the Python default as The CII code to encode.

Read the notes on how Python3 is solving tricky character coding problems

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.