本文主要是介绍第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import re
def get_filters(path):if path is None:returnfilters = ["北京", "程序员", "公务员", "领导", "牛比", "牛逼", "你娘", "你妈", "love", "sex", "jiangge"]with open(path,encoding="utf-8") as f:line = f.read()# for line in (open(path, "w", encoding="utf-8").readlines()):if "\n" in line:filters.append(line[:-1])else:filters.append(line)f.close()return filtersdef main():filters = get_filters("1.txt")while True:temp = input("input:")if temp == "0":print("Exit")breakfor filter_word in filters:new_str = ""if filter_word in temp:if len(re.findall(u"[\u4e00-\u9fa5]+",filter_word)) > 0:len_new_str = len(filter_word)else:len_new_str = 1for i in range(len_new_str):new_str += "*"temp = str(temp).replace(filter_word, new_str)print(temp)if __name__ == "__main__":main()
这篇关于第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!