Is there a way to load sheets with a specific regex with pandas.read_excel()
?
Maybe with the parameter sheet_name
But can't figure how...
Example of regex :
regex_sheet = "\s*[d^D][A^a][y^Y]\D*\s*[1-9]\s*"
CodePudding user response:
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}