我有一些需要取消转义的转义字符串。我想用 Python 来做这件事。
例如,在 Python 2.7 中我可以这样做:
>>> "\\123omething special".decode('string-escape')
'Something special'
>>>
我如何在 Python 3 中做到这一点?这不起作用:
>>> b"\\123omething special".decode('string-escape')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
LookupError: unknown encoding: string-escape
>>>
我的目标是能够采用这样的字符串:
s\000u\000p\000p\000o\000r\000t\000@\000p\000s\000i\000l\000o\000c\000.\000c\000o\000m\000
并将其变成:
"support@psiloc.com"
完成转换后,我将检查我拥有的字符串是以 UTF-8 还是 UTF-16 编码的。
原文由 vy32 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果你想要转义序列的 str 到 str 解码,那么输入和输出都是 Unicode:
测试: