最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python按條件批量刪除TXT文件行工具

 更新時(shí)間:2024年12月28日 15:46:45   作者:hvinsion  
這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)按條件批量刪除TXT文件中行的工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1.簡(jiǎn)介

一個(gè)由Python編寫的可根據(jù)TXT文件按條件批量刪除行工具,資源及文件已打包成exe文件

功能:

  • 批量刪除行含關(guān)鍵字或詞的行(多個(gè)關(guān)鍵字/詞中間用空格隔開)
  • 批量刪除空行
  • 批量字符小于多少(可設(shè)定)刪除行
  • 批量刪除匹配正則的行

使用方法:

  • 點(diǎn)擊打開文件批量選擇TXT文件(可以直接拖拽)。
  • 需要的功能前打勾,并配置。
  • 點(diǎn)擊【執(zhí)行】即可進(jìn)行轉(zhuǎn)換。
  • 最后會(huì)生成原文件名+_new.txt的文件。

2.運(yùn)行效果

3.相關(guān)源碼

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 組件時(shí)需要本語句
import windnd
 
ui_pos = {
    "title": "TXT文件處理助手",
    "geometry": "450x300",  # 長(zhǎng)乘寬
 
}
 
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:  # 循環(huán)遍歷文件
            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: 每一行的內(nèi)容
                # 操作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()  # 關(guān)閉文件
    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()  # 設(shè)定窗體變量
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='選項(xiàng)')
ctrl_Frame1.place(x=14, y=72, width=388, height=140)
 
ctrl_StartBtn = Button(root, text='執(zhí)行', font=('宋體', '9'),
                       command=ctrl_StartBtn_clicked)  # 可在括號(hào)內(nèi)加上調(diào)用函數(shù)部分 ,command=ctrl_StartBtn_clicked
ctrl_StartBtn.place(x=22, y=250, width=72, height=29)
 
ctrl_QuitBtn = Button(root, text='清除', font=('宋體', '9'), command=clearAll)  # 可在括號(hào)內(nèi)加上調(diào)用函數(shù)部分 ,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='刪除行含關(guān)鍵字或詞的行', variable=int_CheckBox1, font=('宋體', '9'))
ctrl_CheckBox1.place(x=14, y=14, height=22)  # 考慮到對(duì)齊問題,不列入寬度,需要時(shí)手動(dòng)加入 width=130
ctrl_CheckBox1.deselect()  # 默認(rèn)為未選中狀態(tài)
 
Ctrl_Label1 = Label(ctrl_Frame1, text="關(guān)鍵字:")
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)  # 考慮到對(duì)齊問題,不列入寬度,需要時(shí)手動(dòng)加入 width=130
ctrl_CheckBox2.deselect()  # 默認(rèn)為未選中狀態(tài)
 
int_CheckBox3 = IntVar()  # 綁定變量
ctrl_CheckBox3 = Checkbutton(ctrl_Frame1, text='刪除字符小于N的行', variable=int_CheckBox3, font=('宋體', '9'))
ctrl_CheckBox3.place(x=14, y=58, height=22)  # 考慮到對(duì)齊問題,不列入寬度,需要時(shí)手動(dòng)加入 width=130
ctrl_CheckBox3.deselect()  # 默認(rèn)為未選中狀態(tài)
# N標(biāo)簽
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)  # 考慮到對(duì)齊問題,不列入寬度,需要時(shí)手動(dòng)加入 width=130
ctrl_CheckBox4.deselect()  # 默認(rèn)為未選中狀態(tài)
 
# N標(biāo)簽
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)  # 可在括號(hào)內(nèi)加上調(diào)用函數(shù)部分 ,command=ctrl_OpenFileBtn_clicked
ctrl_OpenFileBtn.place(x=305, y=18, width=72, height=29)
 
root.mainloop()

到此這篇關(guān)于Python按條件批量刪除TXT文件行工具的文章就介紹到這了,更多相關(guān)Python批量刪除TXT內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python中subprocess模塊用法實(shí)例詳解

    Python中subprocess模塊用法實(shí)例詳解

    這篇文章主要介紹了Python中subprocess模塊用法,實(shí)例分析了subprocess模塊的相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • Python裝飾器用法與知識(shí)點(diǎn)小結(jié)

    Python裝飾器用法與知識(shí)點(diǎn)小結(jié)

    這篇文章主要介紹了Python裝飾器用法與知識(shí)點(diǎn),總結(jié)分析了Python 裝飾器的基本概念、原理、用法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-03-03
  • 對(duì)Django中static(靜態(tài))文件詳解以及{% static %}標(biāo)簽的使用方法

    對(duì)Django中static(靜態(tài))文件詳解以及{% static %}標(biāo)簽的使用方法

    今天小編就為大家分享一篇對(duì)Django中static(靜態(tài))文件詳解以及{% static %}標(biāo)簽的使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • pandas中quantile()函數(shù)的應(yīng)用及說明

    pandas中quantile()函數(shù)的應(yīng)用及說明

    本文介紹了pandas中quantile()函數(shù)用于計(jì)算DataFrame或Series中數(shù)值型數(shù)據(jù)的分位數(shù),通過示例展示了如何計(jì)算整個(gè)DataFrame、每列和每行的分位數(shù),并可指定百分位數(shù)和axis參數(shù)進(jìn)行計(jì)算
    2026-05-05
  • Numpy中的shape函數(shù)的用法詳解

    Numpy中的shape函數(shù)的用法詳解

    這篇文章主要介紹了Numpy中的shape函數(shù)的用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python實(shí)現(xiàn)求最長(zhǎng)回文子串長(zhǎng)度

    python實(shí)現(xiàn)求最長(zhǎng)回文子串長(zhǎng)度

    最長(zhǎng)回文子串問題:給定一個(gè)字符串,求它的最長(zhǎng)回文子串長(zhǎng)度。如果一個(gè)字符串正著讀和反著讀是一樣的,那它就是回文串。今天我們就來探討下這個(gè)問題
    2018-01-01
  • 520必備!這些Python表白代碼祝你脫單成功

    520必備!這些Python表白代碼祝你脫單成功

    不會(huì)還有程序猿沒有女朋友吧?沒關(guān)系,今天特地為大家整理了這些Python花式表白代碼,你就放心大膽的去吧,需要的朋友可以參考下
    2021-05-05
  • python中的tcp示例詳解

    python中的tcp示例詳解

    這篇文章主要給大家介紹了關(guān)于python中tcp協(xié)議的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • python創(chuàng)建子類的方法分析

    python創(chuàng)建子類的方法分析

    這篇文章主要介紹了python創(chuàng)建子類的方法,結(jié)合實(shí)例形式分析了Python子類的具體定義與使用方法,需要的朋友可以參考下
    2019-11-11
  • Python腳本實(shí)現(xiàn)批量導(dǎo)出網(wǎng)站指定文章

    Python腳本實(shí)現(xiàn)批量導(dǎo)出網(wǎng)站指定文章

    這篇文章主要為大家詳細(xì)介紹了如何利用Python腳本實(shí)現(xiàn)批量導(dǎo)出網(wǎng)站指定文章,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2026-02-02

最新評(píng)論

新化县| 水富县| 横峰县| 上犹县| 新安县| 芮城县| 嫩江县| 梓潼县| 信丰县| 平凉市| 渭南市| 长顺县| 吉首市| 县级市| 通道| 聂荣县| 山东省| 睢宁县| 怀宁县| 西丰县| 读书| 海阳市| 广东省| 陈巴尔虎旗| 沛县| 垣曲县| 同德县| 西林县| 班戈县| 五莲县| 奉节县| 集贤县| 文安县| 石林| 黄大仙区| 乡宁县| 璧山县| 怀仁县| 九龙坡区| 延庆县| 沭阳县|