简体   繁体   中英

Read excel sheet in pandas with different sheet names in a pattern

I am trying to read multiple excel files in a loop using read_excel :

Different excel files contain sheet names which contain the word "staff" eg Staff_2013 , Staff_list etc

Is there a way to read all these files dynamically using some wild card concept ?

Something like the code below :

df = pd.read_excel(folder,col_names=True,sheet_name='Staff*')

The pandas.read_excel can only read one sheet. The use you are suggesting would be problematic in case of multiple matches.

So you have to list the sheets and select the ones you want to read one by one.

For instance:

xls_file = pd.ExcelFile('my_excel_file.xls')
staff_fnames = [sheet for sheet in xls.sheet_names if sheet.startswith('Staff')]
for staff_fname in staff_fnames:
    df = pd.read_excel('my_excel_file.xls'), sheet_name=staff_fname)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM