【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('
'):
        if i in userinput:
            userinput = userinput.replace(str(i), '*'*len(i))
    print(userinput)

filtertext('word.txt')

结果:

myinput:我是住在北京的程序员。
我是住在**的***。
原文地址:https://www.cnblogs.com/liez/p/5367656.html