文章由来:
由于运维岗位事情相对于不多,然后看剪辑他们把文件里的符号删掉然后回车,看的心太累,索性给他们写个小工具用用,中间出了很多坑,最终给他们写出来了
下面不读说了,老样子 — 上代码
# coding:utf-8
import tkinter as tk
from tkinter import filedialog
import pyperclip as pcp
import os, re
"""
author : soliton
Email :
"""
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.filePath = tk.StringVar()
self.pack()
# 获取文件
self.getFile_bt = tk.Button(self)
self.getFile_bt["width"] = 15
self.getFile_bt["text"] = "选择文件"
self.getFile_bt["command"] = self._getFile
self.getFile_bt.grid(row=0, column=1)
# 显示文件路径
self.filePath_en = tk.Entry(self, bd=7, width=50)
self.filePath_en.grid(row=0, column=0)
self.filePath_en.delete(0, "end")
self.filePath_en.insert(0, "文件路径可复制粘贴到本框")
# 输出框
self.output_file = tk.Text(self, width=54)
self.output_file.grid(row=1, column=0)
self.output_file.delete("end")
# 修改内容按钮
self.Re_Button = tk.Button(self, text="去符号换行", width=15,command=self.Re_content).grid(row=1, column=1, sticky="N")
# 提示标签
self.Re_Tips = tk.Label(self, text="\r\n本软件\r\n仅支持文本文件\r\nauthor:soliton").grid(row=1, column=1, sticky="E")
self.Copy_Content = tk.Button(self, text="复制生成内容", width=15, command=self.buttoncallback).grid(row=1, column=1, sticky="EN", pady=30)
def buttoncallback(self):
pcp.copy(self.output_file.get(1.0, "end"))
def Re_content(self):
self.output_file.delete(1.0, "end")
try:
with open(self.filePath,'r', encoding='utf-8') as f:
content = f.read()
content = re.sub(r'[,|,|.|。|?|\?|;|;|\!|\!|:|:|、|]{1,1000000}', '\n', content)
self.output_file.insert('end', content)
f.close()
except Exception:
with open(self.filePath, 'r', encoding='GBK') as f:
content = f.read()
content = re.sub(r'[,|,|.|。|?|\?|;|;|\!|\!|:|:|、|]{1,1000000}', '\n', content)
self.output_file.insert('end', content)
f.close()
# 打开文件并显示路径
def _getFile(self):
default_dir = r"文件路径"
self.filePath = tk.filedialog.askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser(default_dir)))
self.filePath_en.delete(0, "end")
self.filePath_en.insert(0, self.filePath)
root = tk.Tk()
root.title("文本内容正则去除器")
root.maxsize(500, 350)
root.minsize(500, 350)
if __name__ == "__main__":
app = Application(master=root)
app.mainloop()
刚开始写好了,发给同事不能使用,不是打不开软件,只是选择文件时, open打开编码报错,在这里卡了很久,后来一步一步打印才晓得,本文我没有把输入框的状态设置成disabled状态,使它自己默认,懒得加了,能用就行