以敏感词列表txt文件进行小说的敏感词替换
作者:互联网
敏感词文件末尾需以空白行结尾
import tkinter as tk
import os
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
print("选择小说")
novel_path = filedialog.askopenfilename()
root = tk.Tk()
root.withdraw()
print("选择敏感词列表,注*敏感词一行一个,以空白行结尾")
mgclist_path = filedialog.askopenfilename()
#打开并读取文档
mgc_file = open(mgclist_path,"r",encoding="utf8") #敏感词文档
novel_file = open(novel_path,"r",encoding="utf8") #小说文档
mgclist = mgc_file.readlines() #读取敏感词(表格形式)
novel = novel_file.read() #读取小说
t="*" #定义替换内容
novel_i = 0 #定义初始下标
def pd(mgc,novel,novel_i): #定义判断函数
for mgc_i in range (len(mgc)): #对于敏感词的每一个下标
if mgc[mgc_i] == novel[mgc_i+novel_i]: #执行判断
r = True #返回True
else:
r = False #返回False
return r #返回值
def th(mgc,novel,novel_i): #定义替换函数
while novel_i < len(novel)-len(mgc)+1: #
if pd(mgc,novel,novel_i) == True: #调用判断函数并判断
novel = novel[:novel_i] + t * len(mgc) + novel[novel_i+len(mgc):] #替换
th(mgc,novel,novel_i) #调用替换函数
else:
novel_i += 1 #循环While
return(novel) #返回新novel
for mgc in mgclist: #对于敏感词列表的每一个敏感词
print("正在处理"+mgc)
mgc = mgc[:len(mgc)-1] #去换行符
novel = th(mgc,novel,novel_i) #调用替换函数获得新novel
print("**********************替换完成**********************")
name=os.path.basename(novel_path)
file=open("替换-"+name,"w",encoding="utf8")
file.write(novel)
打包为可执行文件
https://download.csdn.net/download/m0_61082143/34907339
标签:mgc,novel,len,列表,敏感,file,path,txt 来源: https://blog.csdn.net/m0_61082143/article/details/121000990