我收到了json
个数据,其中有一些unicode字符被转义,而其他人没有。
>>> example = r'сло\u0301во'
揭示这些角色的最佳方法是什么?在下面的示例中,函数unescape
看起来像什么?是否有内置函数可以执行此操作?
>>> unescape(example)
сло́во
答案 0 :(得分:0)
此解决方案假定原始字符串中的\u
的每个实例都是unicode转义符:
def unescape(in_str):
"""Unicode-unescape string with only some characters escaped."""
in_str = in_str.encode('unicode-escape') # bytes with all chars escaped (the original escapes have the backslash escaped)
in_str = in_str.replace(b'\\\\u', b'\\u') # unescape the \
in_str = in_str.decode('unicode-escape') # unescape unicode
return in_str
......或在一行......
def unescape(in_str):
"""Unicode-unescape string with only some characters escaped."""
return in_str.encode('unicode-escape').replace(b'\\\\u', b'\\u').decode('unicode-escape')