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