如何使用Python提取Excel工作表数据



我有很多文件夹,每个文件夹都包含1个excel文件,如1Aug2022、2Aug2022…

我想让python读取所有文件夹,只打开excel文件名,如2022年8月19日,excel文件里面有很多表格,如IP-1*****、IP-2*****、IP 3*****。然后转到带有(IP-2******(的表格,提取2列数据。

我如何在python中做到这一点?

您可以使用pandas包:https://pandas.pydata.org/

就是一个例子

import pandas as pd
your_excel_path = "your/path/to/the/excel/file"
data = pd.read_excel(your_excel_path, sheet_name = "19AUG2022") # If you want to read specific sheet's data
data = pd.read_excel(your_excel_path, sheet_name = None) # If you want to read all sheets' data, it will return a list of dataframes

正如弗格斯所说,使用熊猫。

搜索所有目录的代码可能如下所示:

import os
import pandas as pd
directory_to_search = "./"
sheet_name = "IP-2*****"
for root, dirs, files in os.walk(directory_to_search):
for file in files:
if file == "19AUG2022":
df = pd.read_excel(io=os.path.join(root, file), sheet_name=sheet_name)

最新更新