赞
踩
当前python环境:3.9.11
下载第三方库:
pip install pandas
- import pandas as pd
- def test_01():
- # lists=['学生成绩表.xlsx']
-
- # for i in lists:
- dfs = []
- # 从指定的Excel文件中读取数据,跳过前一行,然后去掉最后一行,返回剩余的数据。
- df = pd.read_excel('学生成绩表.xlsx', skiprows=1).iloc[:-1]
- dfs.append(df)
-
- combined_df = pd.concat(dfs, ignore_index=True)
- # 将合并后的 DataFrame 转换为列表
- combined_list = combined_df.values.tolist()
- # 输出合并后的列表
- print(combined_list)
- print(len(combined_list))

- import pandas as pd
- def test_01():
- # lists=['学生成绩表.xlsx']
-
- # for i in lists:
- dfs = []
- # 从指定的Excel文件中读取数据,跳过前一行,然后去掉最后一行,返回剩余的数据。
- df = pd.read_excel('学生成绩表.xlsx', skiprows=1).iloc[:-1]
- dfs.append(df)
-
- combined_df = pd.concat(dfs, ignore_index=True)
- # 将合并后的 DataFrame 转换为列表
- combined_list = combined_df.values.tolist()
- # 输出合并后的列表
- print(combined_list)
- print(len(combined_list))
-
- # 创建一个 DataFrame 对象
- df = pd.DataFrame(combined_list[1:], columns=combined_list[0])
- # 将df中的数据导出到名为:######.xlsx的Excel文件中,不包含索引,从文件的第4行开始写入数据。
- df.to_excel('学生成绩01.xlsx', index=False,startrow=3)
-
- test_01()

表2、表3
- import pandas as pd
- def test_01():
- lists=['学生成绩表.xlsx','学生成绩表2.xlsx','学生成绩表3.xlsx']
- dfs = []
- for file in lists:
- # 从指定的Excel文件中读取数据,跳过前一行,然后去掉最后一行,返回剩余的数据。
- df = pd.read_excel(file, skiprows=1).iloc[:-1]
- dfs.append(df)
-
- combined_df = pd.concat(dfs, ignore_index=True)
- # 将合并后的 DataFrame 转换为列表
- combined_list = combined_df.values.tolist()
- # 输出合并后的列表
- print(combined_list)
- print(len(combined_list))
-
- # 创建一个 DataFrame 对象
- df = pd.DataFrame(combined_list[1:], columns=combined_list[0])
- # 将df中的数据导出到名为:######.xlsx的Excel文件中,不包含索引,从文件的第4行开始写入数据。
- df.to_excel('学生成绩汇总.xlsx', index=False,startrow=3)
-
- test_01()

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。