python3:由未转义字符

时间:2017-12-07 00:12:17

标签: python python-3.x unicode escaping unicode-escapes

我收到了json个数据,其中有一些unicode字符被转义,而其他人没有。

>>> example = r'сло\u0301во'

揭示这些角色的最佳方法是什么?在下面的示例中,函数unescape看起来像什么?是否有内置函数可以执行此操作?

>>> unescape(example)
сло́во

1 个答案:

答案 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')