当用户输入敏感词语,则用星号 * 替换,例如当用户输入「北京是个好城市」,则变成「 ** 是个好城市」
filtered_words.txt
北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge
源码:
def get_words(file_name):
read_file=open(file_name,'r')
words_list=[]
for line in read_file:
words_list.append(line.strip().decode('GBK'))
return words_list
def str_to_unicode(str_input):
if isinstance(str_input,unicode):
return str_input
else:
try:
str_unicode=str_input.decode("utf-8")
return str_unicode
except UnicodeDecodeError as err:
print err
if __name__=='__main__':
words_list = get_words('filtered_words.txt')
while True:
read_input = raw_input('请输入,退出请输入exit->')
read_list = str_to_unicode(read_input)
if read_input.decode('utf-8') == 'exit':
break
for word in words_list:
if word in read_list:
read_list= read_list.replace(word,len(word)*'*')
print read_list #北京人太牛逼了