import re
words = []
f = open(‘sensitive.txt’, ‘r’) # mode=‘r’ 只读就可以了
lines = f.readlines()
for line in lines:
word = line.replace(’\n’, ‘’) # 去’\n’
words.append(word)
f.close()
a = input(“输入需要过滤的文件名:”)
def guolv(a):
word1 = []
b = open(a, ‘r’)
files = b.readlines()
for line in files:
word = line.replace(’\n’, ‘’)
word1.append(word)
return word1
def check(keywords, text):
return re.sub(’|’.join(keywords), ‘*’*len(keywords), text)
keywords = words
text = ‘’.join(guolv(a))
c = input(“请输入过滤后保存的文件名:”)
k = open(c, ‘w’)
k.write(check(keywords, text))
print(check(keywords, text))
需要的同学可以点个赞哈,写作不易,多多支持,有改进的建议也可以在评论区交流交流。