目录
  1. 1. 思路如下:
python替换敏感词

思路如下:

  • 首先,从敏感词文件中读取到敏感词汇,放入容器中
  • 如果有标点符号,则把一些没用的标点符号去掉
  • 然后,获取用户输入,判断输入是否包含敏感词汇, 并输出相对应的结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os


def filter_words(path):
if os.path.isfile(path):
with open(path, 'r') as f:
words = f.read()
words = words.replace(',', '')
words = words.replace('\n', '')
return words


def sense_words():
words = filter_words('filter.txt')
print("敏感字内容如下:")
print("------------------------------------------------------------------")
print(words)
print("------------------------------------------------------------------")
while True:
sentence = input("请输入:")
if sentence == '0':
print('exit')
break
for i in words:
if i in sentence:
replace = ''
for j in range(len(i)):
replace = replace + '*'

sentence = sentence.replace(i, replace)
print(sentence)


if __name__ == "__main__":
sense_words()
文章作者: nocbtm
文章链接: https://nocbtm.github.io/2018/07/31/python替换敏感词/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 nocbtm's Blog
打赏
  • 微信
  • 支付宝