本文介绍了如何取消转义反斜杠转义的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个字符串,它是另一个字符串的反斜杠转义版本.在 Python 中,是否有一种简单的方法可以对字符串进行转义?例如,我可以这样做:
>>>escaped_str = '"你好,\nworld!"'>>>raw_str = eval(escaped_str)>>>打印 raw_str你好,世界!>>>然而,这涉及将(可能不受信任的)字符串传递给 eval(),这是一个安全风险.标准库中是否有一个函数可以接受一个字符串并生成一个没有安全隐患的字符串?
解决方案
>>>print '"Hello,\nworld!"'.decode('string_escape')你好,世界!"
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!"
这篇关于如何取消转义反斜杠转义的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!