如何在python中取消转义反斜杠转义的字符串? [英] How do I un-escape a backslash-escaped string in python?
本文介绍了如何在python中取消转义反斜杠转义的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
>>> escaped_str ='你好,\\\\
world!'
>>> raw_str = eval(escaped_str)
>>>打印raw_str
你好,
世界!
>>>
但是这涉及将(可能不受信任的)字符串传递给具有安全风险的eval()。标准库中有一个函数,它接受一个字符串并产生一个没有安全隐患的字符串。
解决方案
code>>>> print'你好,\\\\
world!'。decode('string_escape')
你好,
world!
Suppose I have a string which is a backslash-escaped version of another string. Is there an easy way, in Python, to unescape the string? I could, for example, do:
>>> escaped_str = '"Hello,\\nworld!"'
>>> raw_str = eval(escaped_str)
>>> print raw_str
Hello,
world!
>>>
However that involves passing a (possibly untrusted) string to eval() which is a security risk. Is there a function in the standard lib which takes a string and produces a string with no security implications?
解决方案
>>> print '"Hello,\\nworld!"'.decode('string_escape')
"Hello,
world!"
这篇关于如何在python中取消转义反斜杠转义的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文