下面的代码应该用来清除frack这个词,还有可能是一个坏词列表。但目前的问题是函数clean_line
。如果文本行的frack超过两次,则只取第一行,对大写字母也不起作用
class Cleaner:
def __init__(self, forbidden_word = "frack"):
""" Set the forbidden word """
self.word = forbidden_word
def clean_line(self, line):
"""Clean up a single string, replacing the forbidden word by *beep!*"""
found = line.find(self.word)
if found != -1:
return line[:found] + "*beep!*" + line[found+len(self.word):]
return line
def clean(self, text):
for i in range(len(text)):
text[i] = self.clean_line(text[i])
example_text = [
"What the frack! I am not going",
"to honour that question with a response.",
"In fact, I think you should",
"get the fracking frack out of here!",
"Frack you!"
]
clean_text = Cleaner().clean(example_text)
for line in example_text: print(line)
假设您只想去掉任何包含
frack
的单词,您可以执行如下代码。如果您还需要去掉尾随空格,那么您需要稍微更改正则表达式。如果您需要了解有关正则表达式的更多信息,我建议您查看regexone.com输出
使用以下简单代码来清除你的行中的坏词:
导致
clean_line
为:相关问题 更多 >
编程相关推荐