Python项目源码57:数据格式转换工具1.0(csv+json+excel+sqlite3)
1.智能路径处理:自动识别并修正文件扩展名,根据转换类型自动建议目标路径,实时路径格式验证,自动补全缺失的文件扩展名。
2.增强型预览功能:使用pandastable库实现表格预览,第三方模块自己安装一下,自动加载前10行数据,支持实时预览更新,自动调整列宽,SQLite表名自动检测。
pip install pandastable
3.改进的UI交互:分栏式布局(左侧控制/右侧预览),自动保存路径建议,智能表名检测(SQLite),实时错误反馈,一键清空预览。
4.使用说明:选择转换类型,浏览选择源文件(自动生成目标路径建议),查看右侧数据预览确认数据正确性,(数据库转换需要输入/选择表名),点击"开始转换"按钮
# -*- coding: utf-8 -*-
# @Author : 小红牛
# 微信公众号:WdPython
import os
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from tkinter.simpledialog import askstring
import sqlite3
import pandas as pd
from pandastable import Tableclass DataConverterApp:def __init__(self, root):self.root = rootself.root.title("智能数据转换工具1.0")self.root.geometry("1200x700")# 初始化变量self.source_path = tk.StringVar()self.target_path = tk.StringVar()self.table_name = tk.StringVar()self.conversion_type = tk.StringVar(value="excel2json")# 文件类型映射self.file_extensions = {"excel": ("Excel文件", ".xlsx"),"json": ("JSON文件", ".json"),"csv": ("CSV文件", ".csv"),"sqlite": ("SQLite数据库", ".db")}# 创建界面self.create_widgets()self.setup_preview_table()def setup_preview_table(self):"""初始化数据预览表格"""self.preview_frame = ttk.Frame(self.preview_container)self.preview_frame.pack(fill=tk.BOTH, expand=True)self.ptable = Table(self.preview_frame, showtoolbar=False, showstatusbar=True)self.ptable.show()def create_widgets(self):# 主容器main_frame = ttk.Frame(self.root, padding=20)main_frame.pack(fill=tk.BOTH, expand=True)# 左侧控制面板control_frame = ttk.Frame(main_frame, width=400)control_frame.pack(side=tk.LEFT, fill=tk.Y)# 右侧预览面板self.preview_container = ttk.LabelFrame(main_frame, text="数据预览", padding=10)self.preview_container.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)# 转换类型选择type_frame = ttk.LabelFrame(control_frame, text="转换类型", padding=10)type_frame.pack(fill=tk.X, pady=5)conversion_types = [("Excel → JSON", "excel2json"),("JSON → Excel", "json2excel"),("CSV → JSON", "csv2json"),("JSON → CSV", "json2csv"),("SQLite → Excel", "sqlite2excel"),("Excel → SQLite", "excel2sqlite"),("SQLite → CSV", "sqlite2csv"),("CSV → SQLite", "csv2sqlite")]for text, value in conversion_types:rb = ttk.Radiobutton(type_frame, text=text, variable=self.conversion_type,value=value, command=self.update_ui)rb.pack(anchor=tk.W, pady=2)# 源文件设置source_frame = ttk.LabelFrame(control_frame, text="源文件设置", padding=10)source_frame.pack(fill=tk.X, pady=5)ttk.Label(source_frame, text="源文件路径:").pack(anchor=tk.W)ttk.Entry(source_frame, textvariable=self.source_path, width=50).pack(side=tk.LEFT, fill=tk.X, expand=True)ttk.Button(source_frame, text="浏览...", command=self.browse_source).pack(side=tk.RIGHT)# 目标文件设置target_frame = ttk.LabelFrame(control_frame, text="目标设置", padding=10)target_frame.pack(fill=tk.X, pady=5)ttk.Label(target_frame, text="目标路径:").pack(anchor=tk.W)ttk.Entry(target_frame, textvariable=self.target_path, width=50).pack(side=tk.LEFT, fill=tk.X, expand=True)ttk.Button(target_frame, text="浏览...", command=self.browse_target).pack(side=tk.RIGHT)# 数据库表名设置self.table_frame = ttk.LabelFrame(control_frame, text="数据库设置", padding=10)ttk.Label(self.table_frame, text="表名:").pack(side=tk.LEFT)ttk.Entry(self.table_frame, textvariable=self.table_name).pack(side=tk.LEFT, fill=tk.X, expand=True)# 操作按钮btn_frame = ttk.Frame(control_frame)btn_frame.pack(fill=tk.X, pady=10)ttk.Button(btn_frame, text="开始转换", command=self.start_conversion).pack(side=tk.LEFT, padx=5)ttk.Button(btn_frame, text="清空预览", command=self.clear_preview).pack(side=tk.LEFT, padx=5)ttk.Button(btn_frame, text="退出", command=self.root.quit).pack(side=tk.RIGHT, padx=5)# 绑定路径变化事件self.source_path.trace_add("write", self.update_preview)self.table_name.trace_add("write", self.update_preview)self.update_ui()def get_conversion_info(self):"""获取当前转换类型信息"""ct = self.conversion_type.get()source_type = ct.split("2")[0]target_type = ct.split("2")[1]return source_type, target_typedef update_ui(self):"""更新界面元素"""source_type, target_type = self.get_conversion_info()# 更新数据库设置可见性if "sqlite" in self.conversion_type.get():self.table_frame.pack(fill=tk.X, pady=5)else:self.table_frame.pack_forget()# 自动更新目标路径后缀current_target = self.target_path.get()if current_target:base, _ = os.path.splitext(current_target)new_ext = self.file_extensions[target_type][1]self.target_path.set(f"{base}{new_ext}")def browse_source(self):"""选择源文件"""source_type, _ = self.get_conversion_info()file_type = self.file_extensions[source_type]path = filedialog.askopenfilename(title="选择源文件",filetypes=[file_type, ("所有文件", "*.*")])if path:self.source_path.set(path)self.auto_suggest_target_path(path)def auto_suggest_target_path(self, source_path):"""自动生成目标路径建议"""source_type, target_type = self.get_conversion_info()base = os.path.splitext(source_path)[0]new_ext = self.file_extensions[target_type][1]suggested_path = f"{base}_converted{new_ext}"self.target_path.set(suggested_path)def browse_target(self):"""选择目标路径"""_, target_type = self.get_conversion_info()file_type = self.file_extensions[target_type]path = filedialog.asksaveasfilename(title="保存目标文件",defaultextension=file_type[1],filetypes=[file_type, ("所有文件", "*.*")])if path:self.target_path.set(path)def clear_preview(self):"""清空预览"""self.ptable.clearTable()self.ptable.model.df = pd.DataFrame()self.ptable.redraw()def load_preview_data(self):"""加载预览数据"""source_path = self.source_path.get()if not source_path:return Nonetry:source_type, _ = self.get_conversion_info()if source_type == "excel":return pd.read_excel(source_path, nrows=10)elif source_type == "csv":return pd.read_csv(source_path, nrows=10)elif source_type == "json":return pd.read_json(source_path).head(10)elif source_type == "sqlite":conn = sqlite3.connect(source_path)table_name = self.table_name.get() or self.detect_table_name(conn)if table_name:return pd.read_sql_query(f"SELECT * FROM {table_name} LIMIT 10", conn)return Noneexcept Exception as e:messagebox.showerror("预览错误", f"无法加载数据: {str(e)}")return Nonedef detect_table_name(self, conn):"""自动检测数据库表名"""cursor = conn.cursor()cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")tables = cursor.fetchall()if tables:return askstring("选择表", "检测到多个表,请选择:", initialvalue=tables[0][0])return Nonedef update_preview(self, *args):"""更新数据预览"""df = self.load_preview_data()if df is not None:self.ptable.model.df = dfself.ptable.redraw()self.ptable.autoResizeColumns()def start_conversion(self):"""执行转换操作"""source_path = self.source_path.get()target_path = self.target_path.get()conversion_type = self.conversion_type.get()table_name = self.table_name.get()try:# 自动修正目标路径后缀_, target_type = self.get_conversion_info()target_ext = self.file_extensions[target_type][1]if not target_path.endswith(target_ext):target_path = f"{os.path.splitext(target_path)[0]}{target_ext}"self.target_path.set(target_path)# 执行转换逻辑if conversion_type == "excel2json":df = pd.read_excel(source_path)df.to_json(target_path, orient='records', indent=4)elif conversion_type == "json2excel":df = pd.read_json(source_path)df.to_excel(target_path, index=False)elif conversion_type == "csv2json":df = pd.read_csv(source_path)df.to_json(target_path, orient='records', indent=4)elif conversion_type == "json2csv":df = pd.read_json(source_path)df.to_csv(target_path, index=False)elif conversion_type == "sqlite2excel":conn = sqlite3.connect(source_path)table_name = table_name or self.detect_table_name(conn)df = pd.read_sql_query(f"SELECT * FROM {table_name}", conn)df.to_excel(target_path, index=False)elif conversion_type == "excel2sqlite":df = pd.read_excel(source_path)conn = sqlite3.connect(target_path)df.to_sql(table_name or "Sheet1", conn, if_exists='replace', index=False)elif conversion_type == "sqlite2csv":conn = sqlite3.connect(source_path)table_name = table_name or self.detect_table_name(conn)df = pd.read_sql_query(f"SELECT * FROM {table_name}", conn)df.to_csv(target_path, index=False)elif conversion_type == "csv2sqlite":df = pd.read_csv(source_path)conn = sqlite3.connect(target_path)df.to_sql(table_name or "CSV_Data", conn, if_exists='replace', index=False)messagebox.showinfo("成功", f"文件已成功保存到:\n{target_path}")self.update_preview()except Exception as e:messagebox.showerror("错误", f"转换失败: {str(e)}")if __name__ == "__main__":root = tk.Tk()app = DataConverterApp(root)root.mainloop()
完毕!!感谢您的收看
----------★★跳转到历史博文集合★★----------
我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具 NumPy Pygame
相关文章:

Python项目源码57:数据格式转换工具1.0(csv+json+excel+sqlite3)
1.智能路径处理:自动识别并修正文件扩展名,根据转换类型自动建议目标路径,实时路径格式验证,自动补全缺失的文件扩展名。 2.增强型预览功能:使用pandastable库实现表格预览,第三方模块自己安装一下&#x…...
TypeScript 中,属性修饰符
在 TypeScript 中,属性修饰符(Property Modifiers)是用于修饰类的属性或方法的关键字,它们可以改变属性或方法的行为和访问权限。TypeScript 提供了三种主要的属性修饰符:public、private 和 protected。此外ÿ…...

雷赛伺服电机
ACM0经济 编码器17位: ACM1基本 编码器23位磁编, ACM2通用 编码器24位光电, 插头定义:...
基础编程题目集 6-8 简单阶乘计算
本题要求实现一个计算非负整数阶乘的简单函数。 函数接口定义: int Factorial( const int N ); 其中N是用户传入的参数,其值不超过12。如果N是非负整数,则该函数必须返回N的阶乘,否则返回0。 裁判测试程序样例: #in…...

【deepseek教学应用】001:deepseek如何撰写教案并自动实现word排版
本文讲述利用deepseek如何撰写教案并自动实现word高效完美排版。 文章目录 一、访问deepseek官网二、输入教案关键词三、格式转换四、word进一步排版 一、访问deepseek官网 官网:https://www.deepseek.com/ 进入主页后,点击【开始对话】,如…...

CH32V208GBU6沁恒绑定配对获取静态地址
从事嵌入式单片机的工作算是符合我个人兴趣爱好的,当面对一个新的芯片我即想把芯片尽快搞懂完成项目赚钱,也想着能够把自己遇到的坑和注意事项记录下来,即方便自己后面查阅也可以分享给大家,这是一种冲动,但是这个或许并不是原厂希望的,尽管这样有可能会牺牲一些时间也有哪天原…...
【C/C++】RPC与线程间通信:高效设计的关键选择
文章目录 RPC与线程间通信:高效设计的关键选择1 RPC 的核心用途2 线程间通信的常规方法3 RPC 用于线程间通信的潜在意义4 主要缺点与限制4.1 缺点列表4.2 展开 5 替代方案6 结论 RPC与线程间通信:高效设计的关键选择 在C或分布式系统设计中,…...
跨线程和跨进程通信还有多种方式对比
📊 常见通信机制对比 通信方式跨线程支持跨进程支持同步/异步性能编程复杂度特点与适用场景SendMessage✅✅(同桌面)同步较高(阻塞)低简单窗口通信、控制PostMessage✅✅(同桌面)异步高低通知、事件触发COM/DCOM✅✅同步/异步中中高系统级服务、进程间服务封装Socket✅…...

RT Thread Studio创建软件和硬件RTC工程
MCU型号:STM32F103RET6 一.配置软件模拟RTC 1.生成一个带串口输出的工程文件,新建RT-Thread项目工程文件。 2.查看电路图中的串口输出管脚,根据STMCubeMx软件可知此串口为USART1,选择芯片型号为STM32F103RET6,控制台…...
Oracle EBS AP发票被预付款核算创建会计科目时间超长
背景 由于客户职能部门的水电、通信和物业等等费用统一管理或对接部门报销费,在报销费的时候,用户把所有费用分摊到各个末级部门,形成AP发票行有上千行, 问题症状 1、用户过账时,请求创建会计科目一直执行20多个小时未完成,只能手工强行取消请求。 2、取消请求以后,从后…...
驱动开发硬核特训 · Day 30(下篇): 深入解析 lm48100q I2C 音频编解码器驱动模型(基于 i.MX8MP)
作者:嵌入式Jerry 视频教程请关注 B 站:“嵌入式Jerry” 一、背景与目标 在本篇中,我们围绕 TI 的 lm48100q 音频编解码器 展开,深入讲解其作为 I2C 外设如何集成至 Linux 内核音频子系统(ASoC)࿰…...
运维--计划任务
计划任务分为一次性和循环性的计划任务 1.date的用法 date 月日时分年 eg:date 100118222023 date:查看时间和日期、修改时间和日期 获取当前日期:date +%F F:日期 获取当前时间:date +%H:%M:%S H:时 M:分 S:秒 获取周几: date +%w w:周 …...

苍穹外卖心得体会
1 登录认证 技术点:JWT令牌技术(JSON Web Token) JWT(JSON Web Token)是一种令牌技术,主要由三部分组成:Header头部、Payload载荷和Signature签名。Header头部存储令牌的类型(如JW…...
Python爬虫实战:获取jd商城最新5060ti 16g显卡销量排行榜商品数据并做分析,为显卡选购做参考
一、引言 1.1 研究目的 本研究旨在利用 Python 爬虫技术,从京东商城获取 “5060ti 16g” 型号显卡的商品数据,并对这些数据进行深入分析。具体目标包括: 实现京东商城的模拟登录,突破登录验证机制,获取登录后的访问权限。高效稳定地爬取按销量排名前 20 的 “5060ti 16g…...
Fedora升级Google Chrome出现GPG check FAILED问题解决办法
https://dl.google.com/linux/linux_signing_key.pub 的 GPG 公钥(0x7FAC5991)已安装 https://dl.google.com/linux/linux_signing_key.pub 的 GPG 公钥(0xD38B4796)已安装 仓库 "google-chrome" 的 GPG 公钥已安装,但是不适用于此软件包。 请检查此仓库的…...
信息系统监理师第二版教材模拟题第三组(含解析)
信息系统监理师模拟题第三组(30题) 监理基础理论 信息系统工程监理的性质是( ) A. 服务性、独立性、公正性、科学性 B. 强制性、营利性、行政性、技术性 C. 临时性、从属性、随意性、主观性 D. 单一性、封闭性、被动性、保守性答案:A 解析:监理具有服务性、独立性、公正…...

Zcanpro搭配USBCANFD-200U在新能源汽车研发测试中的应用指南(周立功/致远电子)
——国产工具链的崛起与智能汽车测试新范式 引言:新能源汽车测试的国产化突围 随着新能源汽车智能化、网联化程度的提升,研发测试面临三大核心挑战:多协议融合(CAN FD/LIN/以太网)、高实时性数据交互需求、复杂工况下…...

青少年抑郁症患者亚群结构和功能连接耦合的重构
目录 1 研究背景及目的 2 研究方法 2.1 数据来源与参与者 2.1.1 MDD患者: 2.1.2 健康对照组: 2.2 神经影像分析流程 2.2.1 图像采集与预处理: 2.2.2 网络构建: 2.2.3 区域结构-功能耦合(SC-FC耦合)…...

SQL手工注入(DVWA)
手工SQL注入攻击的标准思路 Low等级 (1)判断是否存在注入 (2)猜解字段个数 (3)确定字段顺序 (4)获取当前数据库 (5)获取当前数据库中的表 (…...

【大模型系列篇】Qwen3开源全新一代大语言模型来了,深入思考,更快行动
Qwen3开源模型全览 Qwen3是全球最强开源模型(MoEDense) Qwen3 采用混合专家(MoE)架构,总参数量 235B,激活仅需 22B。 Qwen3 预训练数据量达 36T,并在后训练阶段多轮强化学习,将非思…...
第 11 届蓝桥杯 C++ 青少组中 / 高级组省赛 2020 年真题,选择题详细解释
一、选择题 第 2 题 在二维数组按行优先存储的情况下,元素 a[i][j] 前的元素个数计算如下: 1. **前面的完整行**:共有 i 行,每行 n 个元素,总计 i * n 个元素。 2. **当前行的前面元素**:在行内&#x…...

flutter 专题 一百零四 Flutter环境搭建
Flutter简介 Flutter 是Google开发的一个移动跨平台(Android 和 iOS)的开发框架,使用的是 Dart 语言。和 React Native 不同的是,Flutter 框架并不是一个严格意义上的原生应用开发框架。Flutter 的目标是用来创建高性能、高稳定性…...
Android之Button、ImageButton、ChipGroup用法
一 控件名称及UI代码 Button、ImageButton、ChipGroup <?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app=&qu…...
【AI提示词】二八法则专家
提示说明 精通二八法则(帕累托法则)的广泛应用,擅长将其应用于商业、管理、个人发展等领域,深入理解其在不同场景中的具体表现和实际意义。 提示词 # Role: 二八法则专家## Profile - language: 中文 - description: 精通二八法…...

玩玩OCR
一、Tesseract: 1.下载windows版: tesseract 2. 安装并记下路径,等会要填 3.保存.py文件 import pytesseract from PIL import Image def ocr_local_image(image_path):try:pytesseract.pytesseract.tesseract_cmd rD:\Programs\Tesseract-OCR\tesse…...
set autotrace报错
报错: SQL> set autotrace traceonly SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled SP2-0611: Error enabling STATISTICS report原因分析: 根据上面的错误提示“SP2-0618: Cannot find the Session Identifie…...
C++如何设计和实现缓存(cache)来减少对后端存储的访问压力
随着数据量的激增和用户对低延迟、高吞吐量需求的不断提升,如何减少系统瓶颈、提升响应速度成为了开发者的核心挑战之一。在这一背景下,缓存(cache)作为一种关键的技术手段,逐渐成为解决性能问题的核心策略。缓存的本质是通过存储频繁访问的数据或计算结果,减少对后端存储…...

“Everything“工具 是 Windows 上文件名搜索引擎神奇
01 Everything 和其他搜索引擎有何不同 轻量安装文件。 干净简洁的用户界面。 快速文件索引。 快速搜索。 快速启动。 最小资源使用。 轻量数据库。 实时更新。 官网:https://www.voidtools.com/zh-cn/downloads/ 通过网盘分享的文件:Every…...

TIME_WAIT状态+UDP概念及模拟实现服务器和客户端收发数据
目录 一、TIME_WAIT状态存在的原因 二、TIME_WAIT状态存在的意义 三、TIME_WAIT状态的作用 四、UDP的基本概念 4.1 概念 4.2 特点 五、模拟实现UDP服务器和客户端收发数据 5.1 服务器udpser 5.2 客户端udpcil 一、TIME_WAIT状态存在的原因 1.可靠的终止TCP连接。 2.…...
Rust 学习笔记:关于枚举与模式匹配的练习题
Rust 学习笔记:关于枚举与模式匹配的练习题 Rust 学习笔记:关于枚举与模式匹配的练习题以下程序能否通过编译?若能,输出是什么?考虑这两种表示结果类型的方式,若计算成功,则包含值 T;…...