python中将 \\uxxxx转换为 Unicode字符串
今天碰到一个很有意思的问题,需要将普通的 Unicode字符串转换为 Unicode编码的字符串,如下:
将
\\u9500\\u552e
转化为
\u9500\u552e
也就是
销售
。
乍一看感觉挺简单的,用
re
库将前面的反斜杠去掉即可,但是在替换的过程中会抛出如下错误:
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
re.sub(r"(\)\u", r'', t)
File "D:\Python36\lib\re.py", line 191, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "D:\Python36\lib\re.py", line 301, in _compile
p = sre_compile.compile(pattern, flags)
File "D:\Python36\lib\sre_compile.py", line 562, in compile
p = sre_parse.parse(p, flags)
File "D:\Python36\lib\sre_parse.py", line 855, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
File "D:\Python36\lib\sre_parse.py", line 416, in _parse_sub
not nested and not items))
File "D:\Python36\lib\sre_parse.py", line 765, in _parse
p = _parse_sub(source, state, sub_verbose, nested + 1)
File "D:\Python36\lib\sre_parse.py", line 416, in _parse_sub
not nested and not items))
File "D:\Python36\lib\sre_parse.py", line 502, in _parse