'Formating a string to keep the wanted
I have an issue with this code:
from netmiko import ConnectHandler
from re import search
lsite=[]
try:
NetworkDevice = {
"host" : "ip",
"username" : "username",
"password" : "mdp",
"device_type" : "cisco_ios"
}
connect = ConnectHandler(**NetworkDevice)
connect.enable()
command = "sh cdp nei"
result = connect.send_command(command).strip()
with open(r".\blabla.txt","w") as filout:
filout.write(result)
with open(r"blabla.txt") as temp_f:
datafile = temp_f.readlines()
for line in datafile:
if search("AP", line):
output = (f"{line}")
lsite.append(output.strip())
print(lsite[-1])
except(IndexError):
pass
And I have this output:
056_AP_GRANDE_TERRE.AMCMZ.LAN
Gig 0/12 158 T B I AIR-CAP16 Gig 0
054_AP_GRANDE_TERRE.AMCMZ.LAN
Gig 0/11 132 T B I AIR-CAP16 Gig 0
052_AP_GRANDE_TERRE.AMCMZ.LAN
Gig 0/10 174 T B I AIR-CAP16 Gig 0
050_AP_GRANDE_TERRE.AMCMZ.LAN
Gig 0/9 155 T B I AIR-CAP16 Gig 0
I would like this output (maybe have to split in 2 outputs):
056_AP_GRANDE_TERRE.AMCMZ.LAN
054_AP_GRANDE_TERRE.AMCMZ.LAN
052_AP_GRANDE_TERRE.AMCMZ.LAN
050_AP_GRANDE_TERRE.AMCMZ.LAN
Gig 0/12
Gig 0/11
Gig 0/10
Gig 0/9
I tried many ways with split()
or strip()
but I found nothing which works.
The print(lsite[-1])
is because only printing lsite
shows me every lines who append my list like:
['056_AP_GRANDE_TERRE.AMCMZ.LAN']
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12 123 T B I AIR-CAP16 Gig 0']
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12 123 T B I AIR-CAP16 Gig 0', '054_AP_GRANDE_TERRE.AMCMZ.LAN']
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12 123 T B I AIR-CAP16 Gig 0', '054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11 154 T B I AIR-CAP16 Gig 0',
'052_AP_GRANDE_TERRE.AMCMZ.LAN']
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12 123 T B I AIR-CAP16 Gig 0', '054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11 154 T B I AIR-CAP16 Gig 0',
'052_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/10 142 T B I AIR-CAP16 Gig 0']
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12 123 T B I AIR-CAP16 Gig 0', '054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11 154 T B I AIR-CAP16 Gig 0',
'052_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/10 142 T B I AIR-CAP16 Gig 0', '050_AP_GRANDE_TERRE.AMCMZ.LAN']
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12 123 T B I AIR-CAP16 Gig 0', '054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11 154 T B I AIR-CAP16 Gig 0',
'052_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/10 142 T B I AIR-CAP16 Gig 0', '050_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/9 177 T B I AIR-CAP16 Gig 0']
['056_AP_GRANDE_TERRE.AMCMZ.LAN']
Solution 1:[1]
I hoper this is what you need.
list_of_lists = [['056_AP_GRANDE_TERRE.AMCMZ.LAN'],
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12 123 T B I AIR-CAP16 Gig 0'],
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12 123 T B I AIR-CAP16 Gig 0',
'054_AP_GRANDE_TERRE.AMCMZ.LAN'],
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12 123 T B I AIR-CAP16 Gig 0',
'054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11 154 T B I AIR-CAP16 Gig 0',
'052_AP_GRANDE_TERRE.AMCMZ.LAN'],
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12 123 T B I AIR-CAP16 Gig 0',
'054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11 154 T B I AIR-CAP16 Gig 0',
'052_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/10 142 T B I AIR-CAP16 Gig 0'],
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12 123 T B I AIR-CAP16 Gig 0',
'054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11 154 T B I AIR-CAP16 Gig 0',
'052_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/10 142 T B I AIR-CAP16 Gig 0',
'050_AP_GRANDE_TERRE.AMCMZ.LAN'],
['056_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/12 123 T B I AIR-CAP16 Gig 0',
'054_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/11 154 T B I AIR-CAP16 Gig 0',
'052_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/10 142 T B I AIR-CAP16 Gig 0',
'050_AP_GRANDE_TERRE.AMCMZ.LAN', 'Gig 0/9 177 T B I AIR-CAP16 Gig 0'],
['056_AP_GRANDE_TERRE.AMCMZ.LAN']]
lans = set()
gigs = set()
for outer in list_of_lists:
for inner in outer:
if ".LAN" in inner:
lans.add(inner)
else:
gigs.add(inner.split(" ")[0])
for lan in lans:
print(lan)
for gig in gigs:
print(gig)
Output:
050_AP_GRANDE_TERRE.AMCMZ.LAN
056_AP_GRANDE_TERRE.AMCMZ.LAN
052_AP_GRANDE_TERRE.AMCMZ.LAN
054_AP_GRANDE_TERRE.AMCMZ.LAN
Gig 0/11
Gig 0/12
Gig 0/9
Gig 0/10
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Phantoms |