吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 10256|回复: 107
上一主题 下一主题
收起左侧

[原创工具] 【原创 开源】TXT文件按条件批量删除行工具

    [复制链接]
跳转到指定楼层
楼主
Loker 发表于 2021-8-1 21:32 回帖奖励
本帖最后由 Loker 于 2021-8-3 08:57 编辑

某坛友@排排坐 发的一个帖子【[资源求助] 求TXT文件按条件批量删除行工具】,用Python顺手写了个小工具,现在开源出来,有需要的可以自己修改代码自取。
功能:
  • 批量删除行含关键字或词的行(多个关键字/词中间用空格隔开)
  • 批量删除空行
  • 批量字符小于多少(可设定)删除行
  • 批量删除匹配正则的行

使用方法:
  • 点击打开文件批量选择TXT文件(可以直接拖拽)。
  • 需要的功能前打勾,并配置。
  • 点击【开始 】即可进行转换。
  • 最后会生成原文件名+_new.txt的文件。


2021.08.02 更新:@排排坐  @waltzofjack
  • 批量删除匹配正则的行(方便自定义匹配规则)
  • 允许文件拖拽到界面
  • 执行报错会产生log文件
  • 其他细节变化

使用截图:



GIF图片很大,你忍一下。。。

动图地址GIF地址

打包好的单EXE文件地址(双击即可运行 ):https://wwx.lanzoui.com/iWGXVs4szsd

源码(个把小时写的,质量一般,但是功能很全 ):
[Python] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import re
import time
from tkinter import ttk, filedialog, messagebox, INSERT, Tk, Button, Text, Scrollbar, \
    HORIZONTAL, VERTICAL, IntVar, Checkbutton, Label, StringVar, Entry  # 有Combobox、LabelFrame 组件时需要本语句
import windnd
 
ui_pos = {
    "title": "TxT文件处理助手V0.2-0802 By 52poje Loker",
    "geometry": "450x300"# 长乘宽
 
}
 
FilePaths = ()
 
 
def clearAll():
    ctrl_FileListBox.delete(1.0, "end"# 清空文件路径
    str_KeyWord.set("")
    str_KeyNum.set("")
 
 
def getTxtFiles():
    global FilePaths
    files = filedialog.askopenfilenames(filetypes=[('text files', '.txt')])
    if files:
        FilePaths = files
        for f_name in files:
            ctrl_FileListBox.insert('end', f_name)
            ctrl_FileListBox.insert(INSERT, '\n')
    else:
        messagebox.showinfo(title='提示', message='没有选择任何文件!')
 
 
def KeyWordScan(keys, s):
    key_words = keys.split(" ")
    t_f = False
    for key_word in key_words:
        if key_word in s:
            t_f = True
    return t_f
 
 
def ctrl_StartBtn_clicked():
    has_key_words = int_CheckBox1.get()
    key_words = str_KeyWord.get()
 
    has_empty_line = int_CheckBox2.get()
 
    has_N = int_CheckBox3.get()
    n = str_KeyNum.get()
 
    has_zz = int_CheckBox4.get()
    zz = str_zz.get()
    try:
        for file in FilePaths:  # 循环遍历文件
            s_file = open(os.path.splitext(file)[0] + "_new" + os.path.splitext(file)[1], 'w+'# 文件保存位置
            f_lines = open(file, encoding='utf8').readlines()  # 打开文件,读入每一行
            for s in f_lines:  # s: 每一行的内容
                # 操作1
                if has_key_words:
                    if KeyWordScan(key_words, s):
                        continue
                # 操作2
                if has_empty_line:
                    if len(s.strip()) == 0:
                        continue
                # 操作3:
                if has_N:
                    if len(s.strip()) < int(n):
                        continue
                if has_zz:
                    if re.match(zz, s.strip()):
                        continue
                s_file.write(s)
            s_file.close()  # 关闭文件
    except Exception as e:
        with open("log", "a+") as f:
            f.write(time.strftime("%Y-%m-%d, %H:%M:%S", time.localtime()) + "\n")
            f.write(repr(e) + "\n")
 
 
def draggedFiles(files):
    msg = '\n'.join((item.decode('gbk') for item in files))
    for f_name in files:
        ctrl_FileListBox.insert('end', f_name)
        ctrl_FileListBox.insert(INSERT, '\n')
    print(msg)
 
 
root = Tk()  # 设定窗体变量
root.geometry(ui_pos["geometry"])  # 格式('宽x高+x+y')其中x、y为位置
root.title(ui_pos["title"])
windnd.hook_dropfiles(root, func=draggedFiles)
 
ctrl_Frame1 = ttk.LabelFrame(root, text='选项')
ctrl_Frame1.place(x=14, y=72, width=388, height=140)
 
ctrl_StartBtn = Button(root, text='执行', font=('宋体', '9'),
                       command=ctrl_StartBtn_clicked)  # 可在括号内加上调用函数部分 ,command=ctrl_StartBtn_clicked
ctrl_StartBtn.place(x=22, y=250, width=72, height=29)
 
ctrl_QuitBtn = Button(root, text='清除', font=('宋体', '9'), command=clearAll)  # 可在括号内加上调用函数部分 ,command=ctrl_QuitBtn_clicked
ctrl_QuitBtn.place(x=108, y=250, width=72, height=29)
 
ctrl_FileListBox = Text(root, font=('宋体', '9'))
ctrl_FileListBox.place(x=14, y=7, width=260, height=38)
ctrl_Scrollbar1 = Scrollbar(root, command=ctrl_FileListBox.xview, orient=HORIZONTAL)
ctrl_Scrollbar1.place(x=14, y=46, width=261, height=16)
ctrl_Scrollbar2 = Scrollbar(root, command=ctrl_FileListBox.yview, orient=VERTICAL)
ctrl_Scrollbar2.place(x=275, y=7, width=16, height=39)
ctrl_FileListBox.config(xscrollcommand=ctrl_Scrollbar1.set, yscrollcommand=ctrl_Scrollbar2.set, wrap='none')
 
int_CheckBox1 = IntVar()  # 绑定变量
ctrl_CheckBox1 = Checkbutton(ctrl_Frame1, text='删除行含关键字或词的行', variable=int_CheckBox1, font=('宋体', '9'))
ctrl_CheckBox1.place(x=14, y=14, height=22# 考虑到对齐问题,不列入宽度,需要时手动加入 width=130
ctrl_CheckBox1.deselect()  # 默认为未选中状态
 
Ctrl_Label1 = Label(ctrl_Frame1, text="关键字:")
Ctrl_Label1.place(x=180, y=14, width=55, height=22)
 
str_KeyWord = StringVar()  # 绑定变量
ctrl_KeyWord = Entry(ctrl_Frame1, textvariable=str_KeyWord, font=('宋体', '9'))
ctrl_KeyWord.place(x=230, y=14, width=150, height=22)
 
int_CheckBox2 = IntVar()  # 绑定变量
ctrl_CheckBox2 = Checkbutton(ctrl_Frame1, text='删除空行', variable=int_CheckBox2, font=('宋体', '9'))
ctrl_CheckBox2.place(x=14, y=36, height=22# 考虑到对齐问题,不列入宽度,需要时手动加入 width=130
ctrl_CheckBox2.deselect()  # 默认为未选中状态
 
int_CheckBox3 = IntVar()  # 绑定变量
ctrl_CheckBox3 = Checkbutton(ctrl_Frame1, text='删除字符小于N的行', variable=int_CheckBox3, font=('宋体', '9'))
ctrl_CheckBox3.place(x=14, y=58, height=22# 考虑到对齐问题,不列入宽度,需要时手动加入 width=130
ctrl_CheckBox3.deselect()  # 默认为未选中状态
# N标签
Ctrl_Label = Label(ctrl_Frame1, text="N =")
Ctrl_Label.place(x=180, y=58, width=55, height=22)
# N
str_KeyNum = StringVar()  # 绑定变量
ctrl_KeyNum = Entry(ctrl_Frame1, textvariable=str_KeyNum, font=('宋体', '9'))
ctrl_KeyNum.place(x=230, y=58, width=30, height=22)
 
int_CheckBox4 = IntVar()  # 绑定变量
ctrl_CheckBox4 = Checkbutton(ctrl_Frame1, text='删除符合正则的行', variable=int_CheckBox4, font=('宋体', '9'))
ctrl_CheckBox4.place(x=14, y=80, height=22# 考虑到对齐问题,不列入宽度,需要时手动加入 width=130
ctrl_CheckBox4.deselect()  # 默认为未选中状态
 
# N标签
Ctrl_Label2 = Label(ctrl_Frame1, text="正则:")
Ctrl_Label2.place(x=180, y=80, width=55, height=22)
# N
str_zz = StringVar()  # 绑定变量
ctrl_zz = Entry(ctrl_Frame1, textvariable=str_zz, font=('宋体', '9'))
ctrl_zz.place(x=230, y=80, width=150, height=22)
 
ctrl_OpenFileBtn = Button(root, text='选择文件',
                          font=('宋体', '9'),
                          command=getTxtFiles)  # 可在括号内加上调用函数部分 ,command=ctrl_OpenFileBtn_clicked
ctrl_OpenFileBtn.place(x=305, y=18, width=72, height=29)
 
root.mainloop()


有其他需求想法请在本帖留言,我会尽量更新。
也欢迎其他小伙伴在开源出其他版本。

免费评分

参与人数 28吾爱币 +29 热心值 +24 收起 理由
nx9000 + 1 我很赞同!
ysjd22 + 1 我很赞同!
nbboydky + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
yean + 1 + 1 谢谢@Thanks!
哲喆哟 + 1 + 1 谢谢@Thanks!
lyslxx + 1 + 1 我很赞同!
远方呢 + 1 我很赞同!
欺负老实人么 + 1 + 1 鼓励转贴优秀软件安全工具和文档!
静晴 + 1 感谢大佬分享
tuitui + 1 + 1 我很赞同!
排排坐 + 1 + 1 等你工具更新稳定
z7138910 + 1 + 1 热心回复!
li_jc + 1 + 1 谢谢@Thanks!
265410 + 1 我很赞同!
twlseiya + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
dxswzs + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
风之暇想 + 7 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
huiker231 + 1 谢谢@Thanks!
shao84314 + 2 + 1 我很赞同!
enzhetongle + 1 + 1 我很赞同!
yanyua + 1 我很赞同!
yanglinman + 1 谢谢@Thanks!
wuxiangren + 1 + 1 大佬能不能支持更多的文件格式,比如PHP,HTML,VUE
cict + 1 + 1 用心讨论,共获提升!
yuehanoo + 1 + 1 谢谢@Thanks!
光头鸠摩智 + 1 谢谢@Thanks!
开心长寿果 + 1 + 1 谢谢@Thanks!
loker嘎嘎 + 1 + 1 感谢大佬原创~支持

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

推荐
shao84314 发表于 2021-8-2 11:39
有这样的一个需求:
从论坛上拷贝的小说或文字,直接粘贴在txt文本里,是按照论坛的宽度自动断行的,有没有办法去除。
例子:
然而要做这一篇速朽的文章,才下笔,便感到万分的困难了。第一是文章的名目。孔子曰,"名不正则言不顺"⑶。这原是应该极注意的。
传的名目很繁多:列传,自传,内传⑷,外传,别传,家传,小传……,而可惜都不合。"列传"么,这一篇并非和许多阔人排在"正史"⑸
里;"自传"么,我又并非就是阿Q。说是"外传","内传"在那里呢?倘用"内传",阿Q又决不是神仙。"别传"呢,阿Q实在未曾有大总统
上谕宣付国史馆立"本传"⑹——虽说英国正史上并无"博徒列传",而文豪迭更司⑺也做过《博徒别传》这一部书,但文豪则可,在我辈却
不可。其次是"家传",则我既不知与阿Q是否同宗,也未曾受他子孙的拜托;或"小传",则阿Q又更无别的"大传"了。总而言之,这一篇
也便是"本传",但从我的文章着想,因为文体卑下,是"引车卖浆者流"所用的话⑻,所以不敢僭称,便从不入三教九流的小说家⑼所谓"闲
话休题言归正传"这一句套话里,取出"正传"两个字来,作为名目,即使与古人所撰《书法正传》⑽的"正传"字面上很相混,也顾不得了。
推荐
submariner 发表于 2021-8-6 16:23
本帖最后由 submariner 于 2021-8-6 16:27 编辑

用Python3.9不通过,提示:
line 6, in <module>
    import windnd
ModuleNotFoundError: No module named 'windnd'
用下面的方法,成功了
pip install windnd
Collecting windnd
  Downloading windnd-1.0.7-py3-none-any.whl (4.3 kB)
Installing collected packages: windnd
Successfully installed windnd-1.0.7
WARNING: You are using pip version 21.2.1; however, version 21.2.2 is available.
You should consider upgrading via the 'c:\users\administrator\appdata\local\programs\python\python39\python.exe -m pip install --upgrade pip' command.
头像被屏蔽
沙发
光头鸠摩智 发表于 2021-8-1 21:40
3#
lxzxcvbnmlx 发表于 2021-8-1 21:50
给大神磕头
头像被屏蔽
4#
xkh5823 发表于 2021-8-1 22:01
提示: 作者被禁止或删除 内容自动屏蔽
5#
rrxfw 发表于 2021-8-1 22:05
这个牛,试试看
6#
 楼主| Loker 发表于 2021-8-1 22:06 |楼主
xkh5823 发表于 2021-8-1 22:01
能不能增加多个条件来执行删除的,比如删除所有纯数字/纯小写字母/纯大写/存符号并小于/大于/等于N位数的行 ...

很好的提议,明天更新下。可以再实现个正则或者通配符之类的。
头像被屏蔽
7#
xkh5823 发表于 2021-8-1 22:11
提示: 作者被禁止或删除 内容自动屏蔽
8#
cict 发表于 2021-8-1 22:19
可以新增一个直接选择文件夹的功能
然后添加一个文件后缀名的输入框

通过 os.listdir() 来遍历符合文件条件的文件,减少手动选择文件的操作
9#
pengtusheng 发表于 2021-8-1 22:38
看着就晕                       
10#
waltzofjack 发表于 2021-8-1 23:02
Loker 发表于 2021-8-1 22:06
很好的提议,明天更新下。可以再实现个正则或者通配符之类的。

大佬,到时候可以把正则附在下面,有时会用到
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-28 13:56

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表