Oct-06-2023, 11:22 AM
Fairly new to Python, having some issues with lists.
I am trying to scrape a Cisco Switch config file
So I am splitting the file using ! and the entries are then appending to an empty list.
This bit seems to work ok, I can see the 196 list entries that it creates.
I am then trying to copy the list to another list to work on it, removing any entry that doesn't contain the string 'GigabitEthernet'
It is doing something as the new list is only 98 entries long, but when I look at it, it seems to have removed every even entry in the list.
config list:
[(0, 'Current configuration : 33940 bytes\n'), (1, '\n'), (2, ' Last configuration change at 07:09:55 zone Thu May 5 2022 by \n'), (3, ' NVRAM config last updated at 07:10:13 zone Thu May 5 2022 by \n'), ...
working_config:
[(1, '\n'), (3, ' NVRAM config last updated at 07:10:13 zone Thu May 5 2022 by y\n'), ...
Any Ideas ? I'm obviously doing something wrong, just cant see what it is.
I am trying to scrape a Cisco Switch config file
So I am splitting the file using ! and the entries are then appending to an empty list.
This bit seems to work ok, I can see the 196 list entries that it creates.
I am then trying to copy the list to another list to work on it, removing any entry that doesn't contain the string 'GigabitEthernet'
It is doing something as the new list is only 98 entries long, but when I look at it, it seems to have removed every even entry in the list.
config list:
[(0, 'Current configuration : 33940 bytes\n'), (1, '\n'), (2, ' Last configuration change at 07:09:55 zone Thu May 5 2022 by \n'), (3, ' NVRAM config last updated at 07:10:13 zone Thu May 5 2022 by \n'), ...
working_config:
[(1, '\n'), (3, ' NVRAM config last updated at 07:10:13 zone Thu May 5 2022 by y\n'), ...
Any Ideas ? I'm obviously doing something wrong, just cant see what it is.
# declare an empty list config = [] # open the file with open(r"c:\\Temp\python\switch\switch.txt", mode='r') as f: switchports = f.read().split('!') for row in enumerate(switchports): config.append(row) # create a copy of the list working_config = list(config) print(working_config) for x in working_config: if "GigabitEthernet" not in x: working_config.remove(x)