Python Forum

Full Version: Delete list while iterating
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am looping over different worksheet in excel. I wanted to removed any tabs that are start with name "Sheet" and followed by anything. I want to loop over different excel sheet so that I can removed an item from list where excel sheet name starts with "Sheet" while looping. Can anyone help me how to this task in pthon.

for fn in xlsm_files:
        all_dfs=pd.read_excel(fn, sheet_name=None,header=None, engine='openpyxl')
        list_data = all_dfs.keys()
        all_dfs.pop('Date',None)
        all_dfs.pop(r'Sheet[0-9]',None)
when you loop through the sheet names you can use the startswith method on the string
sheet_name = 'sheet something'
if sheet_name.startswith('sheet'):
    print('started with sheet')