我需要做一些非常快速的输入清理,我想基本上转换所有 <, >
至 <, >
.
我想获得与 '<script></script>'.replace('<', '<').replace('>', '>')
相同的结果无需多次迭代字符串。我知道 maketrans
连同str.translate
(即 http://www.tutorialspoint.com/python/string_translate.htm )但这只会从 1 个字符转换为另一个字符。换句话说,一个人不能做这样的事情:
inList = '<>'
outList = ['<', '>']
transform = maketrans(inList, outList)
有没有builtin
可以在单次迭代中完成此转换的函数?
我想使用 builtin
能力,而不是外部模块。我已经知道 Bleach
.
最佳答案
使用 html.escape() - cgi.escape() 在 Python 3 中被弃用
import html
input = '<>&'
output = html.escape(input)
print(output)
<>&
关于Python 输入清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32054891/