自己写的小工具,查询csv文件前三列
的数据互查,
主要目的是 组sql 需要这三个数据互查,
当然从数据库查的更快,
但数据库的连接需要验证权限,能自己搞,先自己搞
速度待优化,没时间搞了,两个同时运行。
使用的时候自己替换一下 2024年****.csv文件
写完检查,发现可以直接用excel筛选,excel查找找不到,得用excel筛选,糙。
import tkinter as tk
from tkinter import ttk, scrolledtext
import pandas as pdclass CSVQueryApp:def __init__(self, root):self.root = rootself.root.title("CSV查询工具")self.root.grid_propagate(False) # 禁用grid的自适应大小# 设置窗口初始大小和位置initial_width = 800initial_height = 600screen_width = root.winfo_screenwidth()screen_height = root.winfo_screenheight()x_position = (screen_width - initial_width) // 2y_position = (screen_height - initial_height) // 2self.root.geometry(f"{initial_width}x{initial_height}+{x_position}+{y_position}")# 输入框self.entry_label = ttk.Label(root, text="输入查询数据:")self.entry_label.grid(row=0, column=0, padx=10, pady=10)self.entry_var = tk.StringVar()self.entry = ttk.Entry(root, textvariable=self.entry_var)self.entry.grid(row=0, column=1, padx=10, pady=10)# 查询按钮self.query_button = ttk.Button(root, text="查询", command=self.query_csv)self.query_button.grid(row=0, column=2, padx=10, pady=10)# 结果显示框self.result_text = scrolledtext.ScrolledText(root, wrap=tk.WORD)self.result_text.grid(row=1, column=0, columnspan=3, padx=10, pady=10, sticky="nsew") # 使用sticky使其铺满空间# 配置窗口大小调整事件root.bind("<Configure>", self.on_window_resize)# 设置网格的调整权重root.columnconfigure(0, weight=1)root.columnconfigure(1, weight=1)root.columnconfigure(2, weight=1)root.rowconfigure(1, weight=1)def query_csv(self):# 从CSV文件中查询数据query_data = self.entry_var.get()if query_data:try:csv_data = pd.read_csv("2024年****.csv") # 替换为你的CSV文件路径result = csv_data[csv_data.apply(lambda row: query_data in str(row), axis=1)]self.display_result(result.iloc[:, :3]) # 仅保留前三列数据except Exception as e:self.display_result(f"错误: {str(e)}")else:self.display_result("请输入查询数据")def display_result(self, result):self.result_text.delete(1.0, tk.END) # 清空之前的结果if isinstance(result, pd.DataFrame):self.result_text.insert(tk.END, result.to_string(index=False))else:self.result_text.insert(tk.END, str(result))def on_window_resize(self, event):# 窗口大小调整事件处理new_width = event.width - 20 # 调整为合适的宽度,可以根据需要调整new_height = event.height - 100 # 调整为合适的高度,可以根据需要调整self.result_text.config(width=new_width, height=new_height)if __name__ == "__main__":root = tk.Tk()app = CSVQueryApp(root)root.mainloop()