Python 输入清理

标签 python forms validation input sanitization

我需要做一些非常快速的输入清理,我想基本上转换所有 <, >&lt;, &gt; .

我想获得与 '<script></script>'.replace('<', '&lt;').replace('>', '&gt;') 相同的结果无需多次迭代字符串。我知道 maketrans连同str.translate (即 http://www.tutorialspoint.com/python/string_translate.htm )但这只会从 1 个字符转换为另一个字符。换句话说,一个人不能做这样的事情:

inList = '<>'
outList = ['&lt;', '&gt;']
transform = maketrans(inList, outList)

有没有builtin可以在单次迭代中完成此转换的函数?

我想使用 builtin能力,而不是外部模块。我已经知道 Bleach .

最佳答案

使用 html.escape() - cgi.escape() 在 Python 3 中被弃用

import html
input = '<>&'
output = html.escape(input)
print(output)

&lt;&gt;&amp;

关于Python 输入清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32054891/

相关文章:

asp.net-mvc - ASP.Net MVC 2 中列表的客户端验证

python - 为什么会出现这个错误 "input_2_1:0 is both fed and fetched"?

python - 当实现两个玩家时 Pygame 会滞后

javascript - HTML表单使用JavaScript从url图像上传文件

forms - 有没有办法在Angular 2 Reactive Forms中创建 'number'输入FormControl

javascript - 验证失败时,首次点击时不会形成表单数据对象;仅在第二次单击时

php - "Cannot modify header information - headers already sent by"验证、提交然后重定向时出现问题

python - Pandas 数据框到 excel 文件中的特定工作表而不会丢失格式

python - 相当于XDialog的跨平台

javascript - 检查字段中的字母和数字字符