格式化字符串以保留想要的
Formating a string to keep the wanted
我对这段代码有疑问:
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
我有这个输出:
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
我想要这个输出(可能必须分成 2 个输出):
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
我用 split()
或 strip()
尝试了很多方法,但我发现没有任何效果。
print(lsite[-1])
是因为只打印 lsite
会显示附加我列表的每一行,例如:
['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']
希望这是你需要的。
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)
输出:
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
我对这段代码有疑问:
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
我有这个输出:
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
我想要这个输出(可能必须分成 2 个输出):
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
我用 split()
或 strip()
尝试了很多方法,但我发现没有任何效果。
print(lsite[-1])
是因为只打印 lsite
会显示附加我列表的每一行,例如:
['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']
希望这是你需要的。
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)
输出:
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