【发布时间】:2021-03-24 04:19:03
【问题描述】:
我正在处理从早期配置收到的以下输出
['Te1/1/1', 'server', 'Ten', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']
['Te1/1/2', 'desc', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']
['Gi1/2/1', 'desc', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']
['Gi2/1/2', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']
['Te2/2/1', 'server', 'notconnect', '301', 'full', '10G', '10Gbase-LR']
['Po120', 'notconnect', 'unassigned', 'auto', 'auto']
['Po121', 'notconnect', '1', 'auto', 'auto']
我想要的是从列表中删除任何字符串,直到找到字符串“已连接”或“禁用”或“未连接”,但第一项除外。
我尝试过以下配置:
regex_chk = re.compile("((?:not)*?connect(?:ed)*|disabled|)")
for item in items:
for i in item[1:]:
if i != regex_chk:
item.remove(i)
print(item)
结果如下:
['Te1/1/1']
['Te1/1/2']
['Gi1/2/1']
['Gi2/1/2']
['Te2/2/1']
['Po120']
['Po121']
而我希望结果是
['Te1/1/1', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']
['Te1/1/2', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']
['Gi1/2/1', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']
['Gi2/1/2', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']
['Te2/2/1', 'notconnect', '301', 'full', '10G', '10Gbase-LR']
['Po120', 'notconnect', 'unassigned', 'auto', 'auto']
['Po121', 'notconnect', '1', 'auto', 'auto']
【问题讨论】:
-
检查您是如何针对正则表达式进行测试的。
i != regex_chk不这样做。 -
只要在你的循环匹配时添加一个中断。