问题描述
解决方案
eval()
根据提供的字符串进行计算
>>> x = 1
>>> eval('x+1')
2
repr()
返回对象打印表示的字符串
>>> s = "\\xe5\\xa5\\xbd"
>>> s
'\\xe5\\xa5\\xbd'
>>> str(s)
'\\xe5\\xa5\\xbd'
>>> repr(s)
"'\\\\xe5\\\\xa5\\\\xbd'"
代码
s = "\\xe5\\xa5\\xbd"
s = eval(repr(s).replace("\\\\", "\\"))
print(s.encode('raw_unicode_escape').decode())
# 好
封装一下
def twoToOneSlash(s: str) -> str:
'''两个斜杠替换为一个斜杠
>>> twoToOneSlash('\\xe5\\xa5\\xbd')
'\xe5\xa5\xbd'
'''
return eval(repr(s).replace("\\\\", "\\"))