敏感詞在文本文件document.txt中,當用戶輸入敏感詞語時,用*號代替並打印出來
document.txt中的文件內容如下:
北京
上海
廣州
深圳
領導
test.py
1 content=input('請輸入: ') # 輸入 2 for word in open('document.txt',encoding='utf8'): 3 fw = word.strip() # 刪除空格''、\n、 \r、 \t 4 if fw in content: # 如果文件中的敏感字在輸入的字符串中 5 content = content.replace(fw,'*'*len(fw)) # 用* 代替敏感字 6 else: 7 print(content)
運行結果如下: