pandas python re模块匹配不同的sheet_name

发布时间 2023-08-04 11:16:10作者: Oops!#
3
 

You can use pandas.ExcelFile to have a peek at the sheet names, then select the sheets to keep with any method (here your regex), finally load with pandas.read_excel:

import re

xl = pd.ExcelFile('filename.xlsx')

regex = re.compile('your_regex')

sheets = [n for n in xl.sheet_names if regex.match(n)]
# ['matching_sheet1', 'matching_sheet2']

dfs = pd.read_excel(xl, sheet_name=sheets)
#  {'matching_sheet1': DataFrame1,
#   'matching_sheet2': DataFrame2}



实例配置:


xl = pd.ExcelFile(raw_file)

regex = re.match('云数据库 RDS','云数据库 RDS MySQL')

sheets = [n for n in xl.sheet_names if n=="云数据库 RDS MySQL" or n=="云数据库 RDS"][0]

print(sheets)

  sheet_name 选择  云数据库 RDS MySQL或者 云数据库 RDS