I have a list with a bunch of lines (created from a 'show cdp neighbors detail from a Cisco switch or router).
I want to perform some processing but only on lines that contain one of the following four strings:
important = (
"show cdp neighbors detail",
"Device ID: ",
"IP address: ",
"Platform: "
)
Right now my iterations work but I am processing over a large number of lines that end up doing nothing. I simply want to skip over the unimportant lines and do my processing over the four lines lines that contain the data that I need.
What I want to do (in English) is:
for line in cdp_data_line:
if any of the lines in 'cdp_data_line' does not contain one of the four strings in 'important', then continue (skip over the line).
After skipping the unwanted lines, then I can test to see which of the four lines I have left and process accordingly:
if all of the four strings in 'important' is not found in any line of cdp_data_line, the continue
if any of those four strings is found in a line,then process:
elif 'show cdp neighbors detail' in line:
do something
elif 'Device ID: ' in line:
do something
elif 'IP address: ' in line:
do something
elif 'Platform: ' in line:
do something
else:
sys.exit("Invalid prompt for local hostname - exiting")
I am trying to do it like this: if any(s in line for line in cdp_data_line for s not in important): continue
PyCharm says it doesn't know what 's' is, even though I found code snippets using this technique that work in PyCharm. Not sure why mine is failing.
There has got to be a succinct way of saying: If string1 or string 2 or string 3 or string 4 is not in cdp_data_line, then skip (continue), otherwise if I find any of those four strings, do something.
Any help would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire