python敏感字处理【小例子】

it2022-05-05  102

当用户输入敏感词语,则用星号 * 替换,例如当用户输入「北京是个好城市」,则变成「 ** 是个好城市」 filtered_words.txt 北京程序员公务员领导牛比牛逼你娘你妈lovesexjiangge 源码: 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 #北京人太牛逼了

 

转载于:https://www.cnblogs.com/facexiaoxi/p/8567330.html


最新回复(0)