Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete list while iterating
#1
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)
Reply
#2
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,512 May-01-2023, 09:06 PM
Last Post: deanhystad
  I cannot delete and the elements from the list quest 4 2,972 May-11-2021, 12:01 PM
Last Post: perfringo
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,271 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138
  python3: iterating through list not working wardancer84 3 2,357 Jul-08-2020, 04:30 PM
Last Post: DPaul
  Indexing problem while iterating list and subtracting lbtdne 2 2,130 May-14-2020, 10:19 PM
Last Post: deanhystad
  iterating a span of a list Skaperen 5 3,050 Dec-29-2019, 08:15 PM
Last Post: Skaperen
  Iterating through a list of strings Ash_Ren 1 2,079 Nov-22-2019, 08:30 PM
Last Post: buran
  delete from list metro17 8 3,504 Nov-19-2019, 06:42 AM
Last Post: buran
  How do I delete symbols in a list of strings? Than999 1 2,277 Nov-16-2019, 09:37 PM
Last Post: ibreeden
  iterating a list of ranges Skaperen 1 2,024 May-22-2019, 07:44 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020