关键字文本过滤器,关键字过滤器,大小写不敏感,可以保存筛
关键字文本过滤器,关键字过滤器,大小写不敏感,可以保存筛
大小写不敏感,可以保存筛选结果
from tkinter import *import tkinter.filedialogfrom datetime import datetime####################################################filepath = ''####################################################def open_file(): global filepath filepath = tkinter.filedialog.askopenfilename(initialdir = 'C:/', \\ filetypes=[("txt files", "*.txt")])####################################################def on_click(): global filepath #clear list box list.delete(0, END) #get check button value save2file = CheckVar.get() if not filepath: label['text'] = "Please select file first!" return else: sKeyWord = tText.get() if sKeyWord == '': label['text'] = "Please input words!" return label['text'] = "search:" + sKeyWord sKeyWord = sKeyWord.lower() #remove space filepath = filepath.strip() #find the last / filename_num = filepath.rfind('/') + 1 #filename = filepath[filename_num : ] src_ofp = open(filepath, "r") if save2file: dest_filepath = filepath[0 : filename_num] now = datetime.now().strftime('%y%m%d_%H%M%S') dest_filepath = dest_filepath + 'KW' + sKeyWord + \\ '_' + now + '.txt' dest_ofp = open(dest_filepath, "w") #separate words by space/tab... lKeyWord = sKeyWord.split() KeyWordsNum = len(lKeyWord) line_num = 0 list_idx = 0 for line in src_ofp.readlines(): line_lower = line.lower() line_num += 1 i = 0 for i in range(0, KeyWordsNum): if line_lower.find(lKeyWord[i]) == -1: continue else: i = KeyWordsNum line = str(line_num) + ' : ' + line list.insert(list_idx, line) list_idx += 1 if save2file: dest_ofp.write(line) src_ofp.close() if save2file: dest_ofp.flush() dest_ofp.close() label['text'] = "Done"####################################################root = Tk(className = 'LogKeyWordFilter V1')root.wm_minsize(640, 480)root.wm_maxsize(640, 480)label = Label(root)label['text'] = 'Default search \\'error\\' words'label.pack()label.place(x = 400, y = 5)tText = StringVar()tText.set('error')entry = Entry(root)entry['textvariable'] = tTextentry.pack()entry.place(x = 160, y = 5, width = 220)CheckVar = IntVar()Cbutton = Checkbutton(root, text = "Save to File", variable = CheckVar, \\ onvalue = 1, offvalue = 0, height=0, \\ width = 20)Cbutton.pack()Cbutton.place(x = 0, y = 5)button = Button(root, height = 2, width = 6)button['text'] = 'Search'button['command'] = on_clickbutton.pack()button.place(x = 200, y = 30)button1 = Button(root, height = 2, width = 6)button1['text'] = 'Open'button1['command'] = open_filebutton1.pack()button1.place(x = 400, y = 30)yscrollbar = Scrollbar(root, orient='vertical')yscrollbar.pack(side = RIGHT, fill=Y)xscrollbar = Scrollbar(root, orient='horizontal')xscrollbar.pack(side = BOTTOM, fill=X)list = Listbox(root, yscrollcommand = yscrollbar.set, \\ xscrollcommand = xscrollbar.set, height = 30, width = 103)list.pack(side = LEFT, fill = BOTH, expand = True)list.place(x = 0, y = 70)#x,y view bounding to list boxxscrollbar.config(command = list.xview)yscrollbar.config(command = list.yview)root.mainloop()#该片段来自于http://byrx.net
相关内容
- python中id()函数的实用研究实例,python实例,>>>
- django 简单显示数据库的内容,django数据库内容,index.h
- python的循环语句while用法范例,pythonwhile,#!/usr/bin/p
- python将字符串转换为datetime类型,pythondatetime,python将字
- python 字符串模板功能,python字符串模板,# method 1te
- 处理各种编码的字符串,处理编码字符串,# file: Unic
- python dict初始化方式,pythondict初始化,python字典dict
- ghost_export 是为Ghost 导出博客文件的一个python小工具,
- 让python json encode datetime类型,jsondatetime,python2.6+ 自
- Python 合并文本文件,,# coding gbk
评论关闭