一键去除txt文本重复行(带界面版)
发布日期:2021-06-29 18:19:15
浏览次数:2
分类:技术文章
本文共 3903 字,大约阅读时间需要 13 分钟。
需求
- 去除txt文件内的重复行
- 保存去除后的文件
- 打开文件选择框
- 打开保存后的文件所处位置
- 如无重复行不进行任何处理
- 带界面,选择文件后界面显示文件内容
- 去除重复行后显示文件内容
- 多线程处理
- 打包成可执行文件exe
- 源码不准超过一百五十行
效果样板
初始界面
测试txt文件
文件大小为200多k,一万多行,开启速度毫无卡顿,处理也无卡顿。
文本内容 开始测试,点击选择文件,进入文件选择窗口。 选择文件后自动读取文件内容,界面可视化文件内容。 选择文件按钮隐藏,替换为去除重复行按钮。点击去除重复行,将结果显示在界面上。 打开文件所在位置,会生成一个前缀为“new_”的新txt文件,后缀与源文文件名一样,可多次生成,并自动覆盖。视频演示
源码
# %%writefile txt_remove.py# -*- coding:utf-8 -*-#----W.Cxk----import tkinterfrom tkinter import filedialogimport os,sys,threadingimport base64from multiprocessing import Processdef read_txt(): global all_lines,result_lines result_lines=[] try: [result_lines.append(i) for i in all_lines if not i in result_lines or i =='\n'] return result_lines,len(all_lines)-len(result_lines) except: pass def write_txt(txt_path,save_path): global result_lines result_lines,line_number=read_txt() if line_number==0: pass else: with open(save_path,"a+") as f: for i in [i for i in result_lines if i !='\n']: f.write(i) def btnCallBack(): global result_lines,save_path,txt_path thread_it(write_txt,txt_path,save_path) strs = ''.join([i for i in result_lines if i !='\n']) text_new.insert(tkinter.INSERT, strs) text_new.configure(state=tkinter.DISABLED) btn1.pack(pady=6) btn.pack_forget() btn2.pack(pady=6)def thread_it(func, *args): '''将函数打包进线程''' # 创建 t = threading.Thread(target=func, args=args) # 守护 !!! t.setDaemon(True) # 启动 t.start() # 阻塞--卡死界面! # t.join() def chiose_txt_file(): global txt_path,save_path,all_lines txt_path="" text_old.configure(state=tkinter.NORMAL) text_new.configure(state=tkinter.NORMAL) text_new.delete('1.0','end') text_old.delete('1.0','end') all_lines=[] root_path = sys.path[0] try: txt_path = tkinter.filedialog.askopenfilename(title=u'选择txt文件',initialdir=(os.path.expanduser(txt_path))) with open(txt_path,"r") as f: all_lines=f.readlines() if "\n" not in all_lines[len(all_lines)-1]: all_lines[len(all_lines)-1]=all_lines[len(all_lines)-1]+"\n" filepath,tempfilename = os.path.split(txt_path) save_path=filepath+os.sep+"new_"+txt_path.split("/")[-1] if os.path.exists(save_path): os.remove(save_path) strs = ''.join(all_lines) text_old.insert(tkinter.INSERT, strs) text_old.configure(state=tkinter.DISABLED) btn1.pack_forget() btn.pack(pady=6) btn2.pack_forget() except Exception as e: passdef open_dir(): global save_path filepath, tempfilename = os.path.split(save_path) os.startfile(filepath)if __name__ == "__main__": global all_lines,result_lines,txt_path,save_path win = tkinter.Tk() win.title("cxk文本去重") #文本转图标 #with open('test.ico','wb') as f: ##此处是base64转图标,文本过长,不放上来了 #f.write(base64.b64decode("AAABAA----sAAAAAAA")) #win.iconbitmap('test.ico') #os.remove('test.ico') sw = win.winfo_screenwidth() #得到屏幕宽度 sh = win.winfo_screenheight() #得到屏幕高度 ww = 550 wh = 550 #窗口宽高 x = (sw-ww) / 2 y = (sh-wh) / 2 #居中显示 win.geometry("%dx%d+%d+%d" %(ww,wh,x,y)) #不可缩放 win.resizable(0, 0) label=tkinter.Label(win,text='原始文件') #图片 label.pack() # height表示的是显示的行数 text_old = tkinter.Text(win, width=75, height=15) text_old.pack(pady=5) label2=tkinter.Label(win,text='去重后文件') #图片 label2.pack() # height表示的是显示的行数 text_new = tkinter.Text(win, width=75, height=15) text_new.pack(pady=5) btn1 = tkinter.Button(win, text='选择txt文件',width=15,command =lambda:thread_it(chiose_txt_file)) btn1.pack(pady=6) btn = tkinter.Button(win, text='去除重复行',width=15,command = lambda:thread_it(btnCallBack)) btn2 = tkinter.Button(win, text='打开文件所在文件夹',width=15,command = lambda:thread_it(open_dir)) win.mainloop()
转载地址:https://cxk-life.blog.csdn.net/article/details/109522688 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月05日 15时58分04秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
KMP 模板
2019-04-30
2264: sequence(KMP)
2019-04-30
CodeForces - 803C Maximal GCD(贪心 + 枚举)
2019-04-30
C++ 运算符重载
2019-04-30
Hash
2019-04-30
POJ-2785 (hash,二分)
2019-04-30
C. Commentator problem
2019-04-30
time(0)转化成时间
2019-04-30
2060 : Minsum Plus(贪心)
2019-04-30
2095 : 我只看看不写题(贪心)
2019-04-30
2049 : 压死骆驼的最后一根稻草 (规律)
2019-04-30
2025 : 简单环路(并查集)
2019-04-30
pair的使用
2019-04-30
判断多边形边界曲线顺/逆时针
2019-04-30
B 简单多边形
2019-04-30
判断能被N整除的字符串
2019-04-30
K-序列求和 (逆元)
2019-04-30
A-ID and password
2019-04-30
B取石子
2019-04-30
C - 水题
2019-04-30