如何使一个类中的函数每个lin多次删除单词

2024-05-14 05:57:14 发布

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

下面的代码应该用来清除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)

Tags: thetextselfcleanlenreturnexampledef
2条回答

假设您只想去掉任何包含frack的单词,您可以执行如下代码。如果您还需要去掉尾随空格,那么您需要稍微更改正则表达式。如果您需要了解有关正则表达式的更多信息,我建议您查看regexone.com

# Using regular expressions makes string manipulation easier
import re

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!"
]

# The pattern below gets rid of all words which start with 'frack'
filter = re.compile(r'frack\w*', re.IGNORECASE)

# We then apply this filter to each element in the example_text list
clean = [filter.sub("", e) for e in example_text]
print(clean)

输出

['What the ! I am not going', 
 'to honour that question with a response.', 
 'In fact, I think you should', 
 'get the   out of here!', 
 ' you!']

使用以下简单代码来清除你的行中的坏词:

line = "frack one Frack two"
bad_word = "frack"    

line = line.lower()

if bad_word in line:
    clean_line = line.replace(bad_word, "")

导致clean_line为:

"one two" 

相关问题 更多 >