如何解码以“%u"开头的unicode字符串.(Python 3中的(百分号+ u) [英] How to decode the unicode string starting with "%u" (percent symbol + u) in Python 3

查看:155
本文介绍了如何解码以“%u"开头的unicode字符串.(Python 3中的(百分号+ u)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一些类似以下的HTML代码:

I get some HTML code like the following:

<new>8003,%u767E%u5723%u5E97,113734,%u4E50%u4E8B%u542E%u6307%u7EA2%u70E7%u8089%u5473,6924743915824,%u7F50,104g,3,21.57,-2.16,0,%u4E50%u4E8B,1</new>

我知道我可以在Notepad ++中找到所有%u"并将其替换为"/u",然后将其粘贴到Python控制台中,以使其能够正确显示中文字符.但是如何在Python中自动做到呢?

I know I can find and replace all the "%u" with "/u" in Notepad++, and then paste it into Python console to let it display correctly in Chinese characters. But how can I do it automatically in Python?

推荐答案

假定您的输入字符串包含"percent -u"编码的字符,我们可以使用正则表达式替换和回调函数来查找和解码它们.

Assuming that your input string contains "percent-u" encoded chracters, we can find and decode them with a regex replace and a callback function.

百分比-u编码将Unicode代码点表示为四个十六进制数字:%u767E 767E ⇒代码点30334⇒百.

Percent-u encoding represents a Unicode code point as four hexadecimal digits: %u767E767E ⇒ codepoint 30334 ⇒ 百.

import re

def hex_to_char(hex_str):
    """ converts a single hex-encoded character 'FFFF' into the corresponding real character """
    return chr(int(hex_str, 16))

s = "<new>8003,%u767E%u5723%u5E97,113734,%u4E50%u4E8B%u542E%u6307%u7EA2%u70E7%u8089%u5473,6924743915824,%u7F50,104g,3,21.57,-2.16,0,%u4E50%u4E8B,1</new>"

percent_u = re.compile(r"%u([0-9a-fA-F]{4})")

decoded = percent_u.sub(lambda m: hex_to_char(m.group(1)), s)

print(decoded)

可打印

<new>8003,百圣店,113734,乐事吮指红烧肉味,6924743915824,罐,104g,3,21.57,-2.16,0,乐事,1</new>

这篇关于如何解码以“%u"开头的unicode字符串.(Python 3中的(百分号+ u)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆