【python小练】0012题

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

 

把上一题的代码改一下就可以咯。

 

Code:

def filtertext(x):
    with open(x, 'r') as f:
      text = f.read()
    userinput = input('myinput:')
    for i in text.split('\n'):
        if i in userinput:
            userinput = userinput.replace(str(i), '*'*len(i))
    print(userinput)

filtertext('word.txt')

 

结果:

myinput:我是住在北京的程序员。
我是住在**的***。

 

posted @ 2016-04-08 14:17  Liez  阅读(338)  评论(1编辑  收藏  举报