第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好

来源:互联网 发布:借贷软件 编辑:程序博客网 时间:2024/06/10 22:54

第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市」。

北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge



程序:

#-*-coding:utf-8-*-

import os


def 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()
阅读全文
0 0
原创粉丝点击