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