漂白车返回

2024-04-28 22:42:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python漂白库来清理用户在网页上输入的数据。我要做的是获取用户数据,用漂白剂清洗并比较清理后的数据是否与原始用户数据不同,如果是,则向用户发出警告进行修复是的。但是我面临一个问题,如果用户在文本区域中输入一些数据并返回回车漂白剂。清洁删除\r原始文本中的内容,比较失败。在

For example:

If user enters abc (clicks enter) def

When we parse the html text box we get abc\r\ndef

and after bleach.clean() I am getting abc\ndef

我不介意在用户输入中返回回车,但由于某些原因漂白剂正在清洗它,我如何防止这种情况发生?在


Tags: 数据用户文本区域警告网页内容for
1条回答
网友
1楼 · 发布于 2024-04-28 22:42:28

在将输入发送到bleach之前,可以通过删除所有carriage-returns对输入进行预清理。这应该能解决你的问题。以下是一些示例用例:

string.translate示例:

from string import maketrans   # Required to call maketrans function.

intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

str = "this is string example....wow!!!"
print str.translate(trantab)

string.translate输出:

^{pr2}$


string.replace示例:

str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");

string.replace输出:

thwas was string example....wow!!! thwas was really string

编辑:您也可以尝试在使用bleach.clean时重写tagskwarg


您也可以在下面查看更多信息:

相关问题 更多 >