【问题标题】:Is there a way to read all excel sheets with a certain name structure in pandas?有没有办法在熊猫中读取所有具有特定名称结构的excel表?
【发布时间】:2021-04-28 04:20:09
【问题描述】:

所以我有一个包含多个工作表的 Excel 文件,称为库存、库存 1、库存 2...一直到库存 10。但是除了库存表之外还有其他工作表,所以我不能只阅读所有工作表excel文件和concat。有没有办法用 pandas 读取所有这些表,清单表,而不单独写表名称?

谢谢

【问题讨论】:

  • 是可以的,下面的链接stackoverflow.com/questions/26521266/…有一个很好的例子,
  • 嗨 hkandpal,谢谢您的回复。不幸的是,我不小心忘记了一个重要的规范,excel 文件中还有其他表格没有那个名称结构,我不想阅读,所以我不能把 filename = none 而只是 concat。我的错,我忘记了那个重要的细节。

标签: excel pandas for-loop automation


【解决方案1】:

这可以过滤和只读标签名称为“inventory*”的标签。

-- 未经测试

import pandas as pd
import re

xl = pd.ExcelFile('file.xlsx')
dataframes = []
for sheet in xl.sheet_names:
    if re.match('inventory*', sheet):  # when matching pattern add the dataframe to the list
        dataframes.append(pd.read_excel('foo.xlsx', sheet_name=sheet))

【讨论】:

    猜你喜欢
    • 2019-10-09
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多