标签:strip class ret odi word rip utf-8 文本文件 span
代码:
def filtered_words(path=‘filtered_words.txt‘): words = [] with open(path, ‘r‘, encoding=‘utf-8‘, newline=‘‘) as f: for line in f: words.append(line.strip()) return words def main(): words = filtered_words() while True: #注意 python3中input相当于python2中raw_input text = input(‘content: ‘).strip() for word in words: if word in text: replace_str = ‘*‘ * len(word) text = text.replace(word,replace_str) print(text)
敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市
标签:strip class ret odi word rip utf-8 文本文件 span
原文地址:http://www.cnblogs.com/hupeng1234/p/6681491.html