第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市」。
北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge
程序:
#-*-coding:utf-8-*-
import osdef filter_words(path):
if os.path.isfile(path):
with open(path,'r') as f:
words=f.read().split()
return words
def sense_words():
words=filter_words('filtered_words.txt')
while True:
sentence=input("请输入:")
if sentence=='0':
print('exit')
break
for i in words:
if i in sentence:
replace=''
for j in range(len(i)):
replace=replace+'*'
sentence=sentence.replace(i,replace)
print(sentence)
sense_words()