def censor(fileName):
censoredFile = open("censored.txt", "w")
for line in open(fileName, "r"):
censoredLine= do_stuff_to_censor_line(line)
censoredFile.write(censoredLine)
简单地说,这里是函数的作用:
1. open the output file
2. go through the input file... for each line:
2.1 figure out what the censored version of the line would be
2.2 write the censored version of the line to the output file
3. close both files (this happens automatically so you dont actually have to call close()
现在对于一行的实际审查...如果您想正确审查内容,仅查看 4 个字母的单词可能还不够强大。这是因为并非所有的顽皮词都是四个字母长。还有一些不调皮的词是四个字母长[例如:'four','long','want','this','help']
def do_stuff_to_censor_line(line):
list_of_naughty_words = ['naughty_word_1','naughty_word_2','etc']
for naughty_word in list_of_naughty_words:
line.replace(naughty_word,'*$#@!')
return line
我将由您来处理不同的大小写...