Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lists
#1
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.


# 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)
buran write Oct-06-2023, 11:23 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Messages In This Thread
Lists - by blake7 - Oct-06-2023, 11:22 AM
RE: Lists - by buran - Oct-06-2023, 11:37 AM
RE: Lists - by blake7 - Oct-06-2023, 11:54 AM
RE: Lists - by blake7 - Oct-06-2023, 12:08 PM
RE: Lists - by buran - Oct-06-2023, 12:09 PM
RE: Lists - by buran - Oct-06-2023, 12:29 PM
RE: Lists - by buran - Oct-06-2023, 12:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 3,293 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 5,028 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

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