【发布时间】:2021-07-10 08:14:45
【问题描述】:
大家好,我正在寻找一种解决方案,将字符串中包含的所有 unicode 转换为相应的 HTML 实体。
例如:
输入: "这是\u+0024。带有\u+0024的字符串。随机\u+0024.unicode"
输出: "这个是 $ 一个带有 $ random $ unicode 的字符串
我目前对这个问题的解决方案如下:
if "\\u+" in my_string:
unicode_code = (label_content.split("\\u+"))[1].split('.')[0]
unicode_to_replace = f"\\u+{unicode_code}."
unicode_string = f"U+{unicode_code}"
html_code = unicode_string.encode('ascii', 'xmlcharrefreplace')
my_string = label_content.replace(unicode_to_replace, html_code)
但是 unicode 字符串没有以正确的方式转换,有什么建议吗?
提前致谢!
【问题讨论】:
-
欢迎来到 Stack Overflow。请花2分钟tour。此外,打开Help center 并至少阅读How to Ask。然后,edit您的问题提供minimal reproducible example。只要求代码的问题太宽泛了,很可能是put on hold or closed。抱歉,Stack Overflow 不是免费的代码编写服务……
-
坦克,我更新了问题。
标签: python html unicode converters