Python notes 6-%u60a0 and \u60a0 similar to Unicode decoding

Source: Internet
Author: User

Sometimes the return value from the interface is encoded in a format similar to "%u4e0a%u6d77%u60a0%u60a0", not the Unicode encoding in Python.

The Unicode encoding inside Python should be this format: \u4e0a\u6d77\u60a0\u60a0

Unicode encoding-python2

1. First look at the Unicode encoding for Python: \u60a0, this is the beginning of the \u, the English is lowercase

# coding:utf-8# 前面加u可以直接打印中文a = u"\u4e0a\u6d77\u60a0\u60a0"print(a)# 字符串需decode成默认unicode编码b = r"\u4e0a\u6d77\u60a0\u60a0"print(b.decode("unicode_escape"))

2. If you add a U in front of the string, meaning to convert to Unicode encoding, if you get a string prototype, then you need to decode decoding to Unicode encoding, Python, the default Unicode encoding name is Unicode_escape

Replace%-python2

1. If this is the code with the percent, first replace, this is the Unicode encoding, although the English characters inside is the size, fortunately here is not case-sensitive.

# coding:utf-8c = "%u4E0A%u6D77%u60A0%u60A0"# 解决办法一:替换%d = c.replace("%", "\\")print(d.decode(‘unicode_escape‘))
Solution Two: UNICHR

1. First cut into a single character, and then converted to Chinese with UNICHR, and then into a string, this is a bit complicated

# coding:utf-8def switch_to_ch(f): ‘‘‘转换成中文‘‘‘ g = f.split("%u")[1:] h = [‘‘+unichr(int(i, 16)) for i in g] return "".join(h)if __name__ == "__main__": f = "%u4e0a%u6d77%u60a0%u60a0" ch = switch_to_ch(f) print(ch)
Python3 decoding

1.python3 default encoding is Unicode, this is not the same as python2, if directly to the string decode will error: attributeerror: ' str ' object has no attribute ' Ecode '

2.python3 first encode into Utf-8 code, and then decode into the default Unicode can be

3. Code reference

# coding:utf-8c = "%u4E0A%u6D77%u60A0%u60A0"# python3解决办法:替换%d = c.replace("%", "\\")print(d.encode("utf-8").decode("unicode_escape"))
Tags: python

Python notes 6-%u60a0 and \u60a0 similar to Unicode decoding

Related Article

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.