我如何在python中浏览unicode转义字符串?

Mat*_*ork 5 python escaping python-3.x python-3.4

Python 3.4

我有一个unicode转义字符串:

> str = 'blah\\x2Ddude'
Run Code Online (Sandbox Code Playgroud)

我想将此字符串转换为unicode非转义版本 'blah-dude'

我该怎么做呢?

roi*_*ppi 10

编码bytes(使用任何编解码器,utf-8可能有效)然后使用unicode-escape以下方法对其进行解码:

s = 'blah\\x2Ddude'

s.encode().decode('unicode-escape')
Out[133]: 'blah-dude'
Run Code Online (Sandbox Code Playgroud)

  • 没有必要调用“encode”函数。您可以只“导入编解码器”,然后调用“codecs.decode(s, 'unicode-escape')” (4认同)