敏感词在文本文件document.txt中,当用户输入敏感词语时,用*号代替并打印出来
document.txt中的文件内容如下:
北京
上海
广州
深圳
领导
test.py
1 content=input('请输入: ') # 输入 2 for word in open('document.txt',encoding='utf8'): 3 fw = word.strip() # 删除空格''、 、 、 4 if fw in content: # 如果文件中的敏感字在输入的字符串中 5 content = content.replace(fw,'*'*len(fw)) # 用* 代替敏感字 6 else: 7 print(content)
运行结果如下: