读取excel中的多个表的数据,并进行数据提取

news/2024/7/21 7:26:08 标签: excel, python, 数学建模
import pandas as pd
import os
from common.common_tool import make_dir_if_not_exists
from conf.path_config import data_dir

pd.set_option('display.max_columns', 1000000)   # 可以在大数据量下,没有省略号
pd.set_option('display.max_rows', 1000000)
pd.set_option('display.max_colwidth', 1000000)
pd.set_option('display.width', 1000000)


# 获取所有数据,并利用字典封装
def get_data_dict(path1):
    df_t1 = pd.read_excel(path1, sheet_name=None, header=1)
    aa = ['23.1.1', '23.1.2', '23.1.3', '23.1.4', '23.1.5', '23.1.6', '23.1.7', '23.1.8', '23.1.9', '23.1.10', '23.1.11', '23.1.12', '23.1.13', '23.1.14', '23.1.15', '23.1.16', '23.1.17', '23.1.18', '23.1.19', '23.1.20', '23.1.21', '23.1.22', '23.1.23', '23.1.24', '23.1.25', '23.1.26', '23.1.27', '23.1.28', '23.1.29', '23.1.30', '23.1.31', '23.2.1', '23.2.2', '23.2.3', '23.2.4', '23.2.5', '23.2.6', '23.2.7', '23.2.8', '23.2.9', '23.2.10', '23.2.11', '23.2.12', '23.2.13', '23.2.14', '23.2.15', '23.2.16', '23.2.17', '23.2.18', '23.2.19', '23.2.20', '23.2.21', '23.2.22', '23.2.23', '23.2.24', '23.2.25', '23.2.26', '23.2.27', '23.2.28', '23.3.1', '23.3.2', '23.3.3', '23.3.4', '23.3.5', '23.3.6', '23.3.7', '23.3.8', '23.3.9', '23.3.10', '23.3.11', '23.3.12', '23.3.13', '23.3.14', '23.3.15', '23.3.16', '23.3.17', '23.3.18', '23.3.19', '23.3.20', '23.3.21', '23.3.22', '23.3.23', '23.3.24', '23.3.25', '23.3.26', '23.3.27', '23.3.28', '23.3.29', '23.3.30', '23.3.31', '23.4.1', '23.4.2', '23.4.3', '23.4.4', '23.4.5', '23.4.6', '23.4.7', '23.4.8', '23.4.9', '23.4.10', '23.4.11', '23.4.12', '23.4.13', '23.4.14', '23.4.15', '23.4.16', '23.4.17', '23.4.18', '23.4.19', '23.4.20', '23.4.21', '23.4.22', '23.4.23', '23.4.24', '23.4.25', '23.4.26', '23.4.27', '23.4.28', '23.4.29', '23.4.30', '23.5.1', '23.5.2', '23.5.3', '23.5.4', '23.5.5', '23.5.6', '23.5.7', '23.5.8', '23.5.9', '23.5.10', '23.5.11', '23.5.12', '23.5.13', '23.5.14', '23.5.15', '23.5.16', '23.5.17', '23.5.18', '23.5.19', '23.5.20', '23.5.21', '23.5.22', '23.5.23', '23.5.24', '23.5.25', '23.5.26', '23.5.27', '23.5.28', '23.5.29', '23.5.30', '23.5.31', '23.6.1', '23.6.2', '23.6.3', '23.6.4', '23.6.5', '23.6.6', '23.6.7', '23.6.8']
    for i, b in df_t1.items():
        if i in aa:
            os.makedirs(os.path.join(data_dir, '2023', i), exist_ok=True)
            b.to_excel(os.path.join(data_dir, '2023', i, i + ".xlsx"), index=False)


if __name__ == '__main__':
    # 获取所有数据,并利用字典封装
    get_data_dict("每日批复明细2023.xlsx")


import pandas as pd
import os
from common.common_tool import make_dir_if_not_exists
from conf.path_config import data_dir

pd.set_option('display.max_columns', 1000000)   # 可以在大数据量下,没有省略号
pd.set_option('display.max_rows', 1000000)
pd.set_option('display.max_colwidth', 1000000)
pd.set_option('display.width', 1000000)


# 获取所有数据,并利用字典封装
def get_data_dict(path):
    all_dir_name = os.listdir(path)
    # all_dir_name.sort(key=lambda x: int(x.split('.')[-2]))
    all_dir_name.sort(key=lambda x: os.path.getctime(os.path.join(path, x)))
    a = []
    b = []
    c = []
    for dir_name in all_dir_name:
        df = pd.read_excel(os.path.join(path, dir_name, dir_name + '.xlsx'), index_col=0)
        a.append(dir_name)
        b.append(df.loc[df.index == '今日需求量', '总计'].values[0])
        c.append(df.loc[df.index == '昨日用气量', '总计'].values[0])
    df1 = pd.DataFrame({'日期': a, '今日需求量': b, '昨日用气量': c})
    df1.to_csv('2023每日批复明细.csv', index=False)


if __name__ == '__main__':
    # 获取所有数据,并利用字典封装
    get_data_dict(os.path.join(data_dir, '2023'))```


http://www.niftyadmin.cn/n/425137.html

相关文章

Homebrew 安装与卸载

Homebrew 安装与卸载 安装方式一(推荐):(使用 科大源 进行安装)安装方式二:(完全纯官方安装,但是需要配置 host,githubusercontent 国内访问不了) 安装方式一(推荐):&…

react + antd实现动态切换主题功能(适用于antd5.x版本)

前言 在之前的几篇文章中(React Antd实现动态切换主题功能之二(默认主题与暗黑色主题切换)、React Antd实现动态切换主题功能)介绍了antd实现动态切换主题功能,文章里介绍的方法在antd5.x版本却不适用,因…

属性和方法

类的属性 变量:1,按照数据类型来分基本数据类型,引用数据类型 2,按照变量在类中声明的位置不同:成员变量(属性)、局部属性(方法内,构造器内,代码块内等&…

私有云和公有云是什么?有什么区别?

作者:Insist-- 个人主页:insist--个人主页 作者会持续更新网络知识和python基础知识,期待你的关注 目录 一、私有云和公有云是什么? 1、私有云是什么? 2、公有云是什么? 二、举个例子 1、私有云 2、公…

u-view2.0 的placeholder-style设置失效, 解决办法

设置placeholder-style属性发现怎么设置都无效,此时更有趣的事情发生了;(使用了uview-ui v1,情况都一样) 查了一些资料,原因是因为动态创建元素的原因, 解决办法:用v-if去控制显隐…

GitHub 下载某个程序的特定版本(代码)

git clone 下载源码 git tag 列出所有版本号 git checkout 某版本号  你当前文件夹下的源码会变成这个版本号的源码。 但可能遇到错误: error: Your local changes to the following files would be overwritten by checkout : xxxx  Please commit your ch…

AutoCV第十课:3D基础

3D基础 前言 手写 AI 推出的全新保姆级从零手写自动驾驶 CV 课程,链接。记录下个人学习笔记,仅供自己参考。 本次课程我们来学习下 nuScenes 数据集的可视化。 课程大纲可看下面的思维导图。 1. nuScenes数据集 明确下我们本次学习的目的:将…

CentOS添加永久路由方式

CentOS添加永久路由方式 1. CentOS7添加永久路由2. CentOS8添加永久路由 1. CentOS7添加永久路由 ● 方式一&#xff1a;   执行以下命令打开路由配置文件进行编辑&#xff1a; vi /etc/sysconfig/network-scripts/route-<interface>这里的<interface>是指您要添…