Python 漂白车返回
我正在使用python漂白库来清理用户在网页上输入的数据。我正在做的是获取用户数据,使用漂白剂清理并比较清理后的数据是否与原始用户数据不同,如果是,则向用户发出警告以进行修复。但我面临的问题是,如果用户使用回车漂白剂在文本区域中输入某些数据,则会删除原始文本中的\r,并且我的比较失败 例如: 如果用户输入 abc(点击输入) def 当我们解析html文本框时,我们会得到abc\r\ndef 漂白后。清洁()我得到abc\n免费Python 漂白车返回,python,python-2.7,Python,Python 2.7,我正在使用python漂白库来清理用户在网页上输入的数据。我正在做的是获取用户数据,使用漂白剂清理并比较清理后的数据是否与原始用户数据不同,如果是,则向用户发出警告以进行修复。但我面临的问题是,如果用户使用回车漂白剂在文本区域中输入某些数据,则会删除原始文本中的\r,并且我的比较失败 例如: 如果用户输入 abc(点击输入) def 当我们解析html文本框时,我们会得到abc\r\ndef 漂白后。清洁()我得到abc\n免费 我不介意用户输入中的回车,但由于某些原因,漂白剂正在清洗它,如何防
我不介意用户输入中的回车,但由于某些原因,漂白剂正在清洗它,如何防止这种情况发生?您可以在将输入发送到
漂白剂之前对其进行预消毒,方法是删除所有回车。那应该能解决你的问题。以下是一些示例用例:
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)
th3s 3s str3ng 2x1mpl2....w4w!!!
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
thwas was string example....wow!!! thwas was really string
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)
th3s 3s str3ng 2x1mpl2....w4w!!!
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
thwas was string example....wow!!! thwas was really string
字符串。替换
示例:
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)
th3s 3s str3ng 2x1mpl2....w4w!!!
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
thwas was string example....wow!!! thwas was really string
字符串。替换
输出:
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)
th3s 3s str3ng 2x1mpl2....w4w!!!
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
thwas was string example....wow!!! thwas was really string
编辑:您也可以尝试在使用漂白剂时覆盖标记
kwarg.clean
此外,您还可以查看以下内容以了解更多信息:
备选地,考虑使用<代码>字符串。翻译< /代码>删除原始文本中的所有<代码> \r>代码> s。对于删除单个字符的情况,<代码> STRE替换(‘R’,‘’’)/代码>可能更可读。@ OozeMeister是个人喜好,但我会更新建议,谢谢。但我想的更多的是,对于其他格式化/缩进字符,如\t\n等,我将不得不单独删除它们,并且可能仍会遗漏一些,是否有更简单的方法强制漂白剂不进行消毒them@Priyam更新了答案