Several ways Python converts Unicode encoding to Chinese characters

Source: Internet
Author: User

str ='\u4eac\u4e1c\u653e\u517b\u7684\u722c\u866b' Method 1 using Unicode_escape decodingPrintStr.decode ('Unicode_escape')PrintUnicode (str,'Unicode_escape') Method 2: If it is in JSON format, use json.loads decodingPrintJson.loads (''%s ''%str)Method 3: Use evalPrintEval'u "%s"'%str)Method 4: Use Python3





Summarize:
1. Str.encode () converts the string to its raw bytes form; Bytes.decode () converts raw bytes to a string form
2. When encountering a similar coding problem, first check the response content text is what type,
If type (text) is bytes, then:
Text.decode (' Unicode_escape ')
If type (text) is STR, then:
Text.encode (' latin-1 '). Decode (' Unicode_escape ')

Several ways Python converts Unicode encoding to Chinese characters

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.