首页 > 其他 > 详细

敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市

时间:2017-04-08 14:01:56      阅读:285      评论:0      收藏:0      [点我收藏+]

代码:

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题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市

原文:http://www.cnblogs.com/hupeng1234/p/6681491.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!