基于pandas实现excel文档的拆分

import glob
import pandas as pd
import tkinter as tk
from tkinter import filedialog


def mkdir(path):
    import os
    path = path.strip()
    path = path.rstrip("\\")
    is_exists = os.path.exists(path)

    # 判断结果
    if not is_exists:
        os.makedirs(path)
        print(path + ' 创建成功')
        return True
    else:
        return False


print('请选择数据文件夹:')

root = tk.Tk()
root.withdraw()

FolderPath = filedialog.askdirectory()  # 看情况自己使用

pd.set_option('display.max_rows', None)
# a new file
# open all the CSV file
# 遍历文件夹下所有csv文件
# TEST_PATH = 'D://桌面//1664329473'
csv_list = glob.glob(f'{FolderPath}\data*.csv')
csv_list_len = len(csv_list)
print('共有%s个CSV文件' % len(csv_list))

if csv_list_len == 0:
    input()

mkdir(FolderPath + '//统计')


def get_data():
    df_list = []
    for csv_file in csv_list:
        df = pd.read_csv(csv_file)
        df_list.append(df)
    df = pd.concat(df_list)
    print("Loading...")
    return df


data = get_data()

one_csv = pd.read_csv(csv_list[0])
th_list = list(one_csv.columns.values)

while True:
    print('-----------------------------')
    for index, item in enumerate(th_list):
        print(f'{index}.{item}')
    num = input("请选择你要分类的字段名称(不能选择按时间分类): ")
    if int(num) >= len(th_list):
        print("序号不存在! 请输入正确的序号")
    else:
        try:
            category = th_list[int(num)]
            res1 = data.groupby(data[category])[category].count()
            res = res1.sort_values(ascending=False)
            group_by_category = data.groupby(data[category])
            try:
                with pd.ExcelWriter(FolderPath + '//统计//all.xlsx') as writer:
                    print("=============>正在统计......")
                    res.to_excel(writer, sheet_name='统计')
                    print("=============>完成统计")
                    for i in group_by_category:
                        try:
                            with pd.ExcelWriter(FolderPath + '//统计//' + str(i[0]) + '.xlsx') as tbWriter:
                                i[1].to_excel(tbWriter, index=False, sheet_name=str(i[0]))
                                print(i[0], "=============>数据处理成功")
                        except Exception as e:
                            print(e)
                            print(i[0], '\033[1;31m==============>数据处理失败\033[0m')
            except PermissionError:
                print('i[0]+\033[1;31m无权限写入excel,请检查是否用word 或者 wps打开,如果打开,请关闭后再运行程序\033[0m')
                input()
            except Exception as e:
                print(e)
                print("数据处理未完成,程序执行错误!!!")
                input()
            print('数据处理完毕!请查看Excel')
            input("请输入任意键退出窗口")
        except Exception as e:
            print(e)
        break

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员禅心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值