如何解析 python 3 中的此类字符串以使用 `\\x` 转换字符串中已经存在的十六进制字符

How parse such string in the python 3 to convert hex character already present in string with `\\x`

示例。

x = "\x20Please\x20try\x20again\x20later"

y = parse_string(x)

print(y)
## It should be converted to "Please try again later".

我试过URLDecoder的unquote方法。我试过编码(“utf-8”)但没有用。我正在考虑查找和替换,因为没有可用的标准函数来处理这些问题。

您必须首先将它转换回字节对象 - 为此您使用“字符映射编码”对其进行编码:即可以提供任何 0-256 代码点字符到字节并返回的“往返”的编码再次。 “latin1”就是这样一种编码。

然后,使用特殊的“unicode 转义”字符编解码器将结果解码回文本 - 这会将“物理”\ 字符解析为转义码的一部分,就像当字符串在源代码中被解析。

抱歉,如果这听起来很复杂 - 实际上它要简单得多:

In [40]: x = "\x20Please\x20try\x20again\x20later"                                                    

In [41]: y = x.encode("latin1").decode("unicode escape")                                                  

In [42]: print(y)         
 Please try again later