Python在实际工作中的运用-通用格式CSV文件自动转换XLSX

继续上篇《Python在实际工作中的运用-CSV无损转XLSX的几个方法》我们虽然对特定格式的CSV实现了快速转换XLSX的目标,但是在运行Py脚本前,还是需要编辑表格创建脚本和数据插入脚本,自动化程度很低,实用性不强,为减少人工提高效率,实现输入CSV文件路径即可自动适配完成转换。现将改进后的脚本发出来,供大家共同交流学习。

脚本说明:

1、本脚本适合字段之间用空格分隔或者用逗号分隔的CSV文本

2、只需将CSV文件路径(含文件名和扩展名)设置到file_path_name变量即可

3、SQlite是中间库,本脚本在执行转换前会自动删除重建。

4、转换的XLSX文件,字段名是自动命名的,请自行修改为自己的表头。

python 复制代码
import os
import sqlite3
import re
import time
import pandas as pd
from pathlib import Path

# 公共变量
# 待转换的CSV文件【必填】
file_path_name = f'{os.path.dirname(__file__)}/待转换的CSV文件.csv'

# 首行是否标题true是false否(默认为否)
isheader = False
# 设置字段之间的分割符号,默认是空格
delimiter = " "
# 获取入口文件目录
file_dirname = os.path.dirname(file_path_name)
# 获取入口文件名(不带扩展名)
file_name = Path(file_path_name).stem

# 删除重建数据库
dbfile_path_name = f'{file_dirname}/{file_name}.db'
if os.path.exists(dbfile_path_name):
    os.remove(dbfile_path_name)
    # 连接到 SQLite 数据库(如果数据库文件不存在,会自动创建一个新的数据库文件)
    conn = sqlite3.connect(dbfile_path_name)
else:
    conn = sqlite3.connect(dbfile_path_name)

cursor = conn.cursor()
sql = f"select name from sqlite_master where name='{file_name}';"
cursor.execute(sql)

# 创建数据表
def CreateTB(conn, header_name = None):
    cursor = conn.cursor()
    # fetchone逐行获取查询结果,如果没有结果返回则判断表未创建
    if not bool(cursor.fetchone()):
        with open(file_path_name,'r',encoding='utf-8') as file:
            # 获取字段个数
            line = file.readline()
            if delimiter == " ":
                line = re.sub(r'\s+',',',line)

            if re.findall('^,',line):
                line = re.sub('^,','',line)

            if re.findall(',$',line):
                line = re.sub(',$','',line)

            # 将整理好的文本分割成字段,并获得字段数
            line_split = line.split(',')
            sql = ""
            if isheader:
                for i in range(len(line_split)):
                    sql += f",{line_split[i]} TEXT NOT NULL"
                    #index_name = line_split[index_num]
                    header_name += f"{line_split[i]},"

                    if i == len(line_split) - 1:
                        header_name = re.sub(',$','',header_name)
            else:
                for i in range(len(line_split)):
                    sql += f",字段名{i} TEXT NOT NULL"
                    #index_name = f"字段名{index_num}"
                    header_name += f"字段名{i},"

                    if i == len(line_split) - 1:
                        header_name = re.sub(',$','',header_name)

            sql = f"CREATE TABLE IF NOT EXISTS {file_name}(id INTEGER PRIMARY KEY{sql});"

            cursor.execute(sql)
            #sql = f"CREATE UNIQUE INDEX IF NOT EXISTS tableindex ON {file_name} ({index_name});"
            #cursor.execute(sql)
            # 提交更改
            conn.commit()
            return header_name

# 调用创建数据表函数
header = CreateTB(conn,"")

with open(file_path_name,'r',encoding='utf-8') as file:
    lines = file.readlines()
    rownum = len(lines)

    i=0
    cursor.execute('BEGIN TRANSACTION')
    start_time = time.time()
    for line in lines:
        # 这里用到正则表达式对数据中存在的多个空格以及数据行前后逗号进行处理
        if delimiter == " ":
            line = re.sub(r'\s+',',',line)

        if re.findall('^,',line):
            line = re.sub('^,','',line)

        if re.findall(',$',line):
            line = re.sub(',$','',line)

        # 对整理完的文本,用逗号进行分割
        line_split = line.split(',')

        # 组装insert插入记录
        fieldsValue = ""
        for j in range(len(line_split)):
            fieldsValue += f"'{line_split[j]}',"
            if j == len(line_split) - 1:
                fieldsValue = re.sub(',$','',fieldsValue)

        try:
            sql = f"INSERT OR ignore INTO {file_name}({header}) VALUES ({fieldsValue});"
            cursor.execute(sql)
        except Exception as e:
            #conn.rollback()
            print(e)

        # 对操作进行计数
        i=i+1
        if i == rownum:
            conn.commit()

            # 在这里将导入的待转换CSV文件数据经过SQLite数据库转化为Excel表导出
            df = pd.read_sql_query(f'select * from {file_name}',conn)
            df.to_excel(f'{file_dirname}/{file_name}.xlsx',index=False)

# 输出执行结果
end_time = time.time()
print(f'共转换{i}行,操作完毕,执行时间:{end_time-start_time}秒')
相关推荐
shykevin1 小时前
python开发Streamable HTTP MCP应用
开发语言·网络·python·网络协议·http
漫路在线1 小时前
JS逆向-某易云音乐下载器
开发语言·javascript·爬虫·python
成功人chen某4 小时前
配置VScodePython环境Python was not found;
开发语言·python
听忆.4 小时前
前端上传获取excel文件后,如何读取excel文件的内容
excel
2301_786964364 小时前
EXCEL Python 实现绘制柱状线型组合图和树状图(包含数据透视表)
python·microsoft·excel
玩电脑的辣条哥5 小时前
怎么在excel单元格1-5行中在原来内容前面加上固定一个字?
excel
skd89995 小时前
小蜗牛拨号助手用户使用手册
python
「QT(C++)开发工程师」5 小时前
STM32 | FreeRTOS 递归信号量
python·stm32·嵌入式硬件
史迪仔01125 小时前
[python] Python单例模式:__new__与线程安全解析
开发语言·python·单例模式
胡耀超5 小时前
18.自动化生成知识图谱的多维度质量评估方法论
人工智能·python·自动化·知识图谱·数据科学·逻辑学·质量评估