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文字の単語を見るだけではおそらく十分に強力ではありません. これは、すべてのいたずらな単語が 4 文字の長さであるとは限らないためです。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
大文字小文字の違いについてはお任せします...