Processing of Python character encoding problems

Source: Internet
Author: User

The problem of character encoding in Python is often a problem that beginners can not understand, in order to clarify this problem, we need to understand the following concepts first

    • The role of decode and encode functions
    • Encoding format for string literals

Decode (str)-converts another encoded string to a Unicode encoded string, returning a string converted to Unicode encoding

Encode (str)-Converts a Unicode string to another encoding (STR) string, returning a string converted to another encoding.

So, the role of decode and encode is to convert other encodings to Unicode and convert Unicode to other encodings.
It is worth noting that the encoding of the object calling decode and the encoding inconsistent with the decode parameter indicate that the function will be faulted, for example, if the STR1 string object itself is encoded in ASCII, but the passed parameter is ' gb2312 ', the function is interrupted.
And when a string object itself is encoded not Unicode but calls the Encode function, the function also goes wrong.

#' Everybody good ' is a string literal, in a different editor, its encoding format is not the same#If the editor holds the text in UTF-8 encoded format, all string literals are in utf-8 format#If the editor holds the text in GB2312 encoded format, all string literals are in ASCII formatSTR1 ='Hello, everyone.' #because the editor saves text in gb2132 encoding, the gb2312 string is first converted to a Unicode-type string # Therefore, STR1 is gb2132 encoded, STR2 is UnicodeSTR2 = Str1.decode ('gb2312') #STR2 is Unicode-encoded, so you can call the Encode functionStr2.encode ("Utf-8")

Summarize

      • When calling the Decode function, be sure to figure out what the code of the string object itself calls this function is, and only then do you know what the formal parameter you are going to pass in.
      • When calling the Encode function, make sure that the string object that invokes the function is Unicode-encoded.

Processing of Python character encoding 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.