python - 如何在python中的键/值对中查找和打印文本
问题描述
我有一个脚本,可以对某些网络设备执行许多不同的测试。我只想从输出中打印失败的消息。密钥(显示 bgp 摘要)不是恒定的,它可以变化。并且可以在“失败”部分和“通过”部分中存在多个按摩领域。我怎样才能做到这一点?
pprint(checkvalue.test_results)
提供此输出:
{'show bgp summary': [{'count': {'fail': 2, 'pass': 8},
'failed': [{'id': {'peer-address': '10.10.20.20'},
'message': ' The BGP Neighbour 10.10.20.20 '
"was in ['Established'], now "
"it's ['Connect']",
'post': {'peer-address': '10.10.20.20',
'peer-state': ['Connect']},
'post_node_value': ['Connect'],
'pre': {'peer-state': ['Established']},
'pre_node_value': ['Established']},
{'id': {'peer-address': '10.10.10.10'},
'message': ' The BGP Neighbour 10.10.10.10 '
"was in ['Established'], now "
"it's ['Connect']",
'post': {'peer-address': '10.10.10.10',
'peer-state': ['Connect']},
'post_node_value': ['Connect'],
'pre': {'peer-state': ['Established']},
'pre_node_value': ['Established']}],
'node_name': 'peer-state',
'passed': [{'id': {'peer-address': '111.111.111.111'},
'message': 'BGP State check',
'post': {'peer-address': '111.111.111.111',
'peer-state': ['Established']},
'post_node_value': ['Established'],
'pre': {'peer-state': ['Established']},
'pre_node_value': ['Established']},
{'id': {'peer-address': '0000:0000:0000:00:2'},
'message': 'BGP State check',
'post': {'peer-address': '0000:000:0000:00::2',
'peer-state': ['Active']},
'post_node_value': ['Active'],
'pre': {'peer-state': ['Active']},
'pre_node_value': ['Active']},
'result': False,
'test_name': 'BGP-peers-estab',
'testoperation': 'no-diff',
'xpath': 'bgp-peer'}]}
我只想打印失败的测试消息。像这样:“BGP 邻居 10.10.10.10”“在 ['Established'] 中,现在““它是 ['Connect']”
解决方案
谢谢大家。你为我指明了正确的方向。我可以通过以下方式实现我的目标:
for testname, value in checkvalue.test_results.items():
for i in checkvalue.test_results[testname][0]['failed']:
print(i['message'])
推荐阅读
- amazon-web-services - AWS EB 多容器 Datadog 集成无法正常工作
- c++ - 如何使用一张地图存储不同的功能
- anchor - 在带有锚标签的手机上的另一个选项卡中打开链接
- python - 有没有办法将 tkinter 中的条目文本框转换为变量
- javascript - 如何访问javascript函数中输入文本字段的值?
- xcode - 如何使用 SwiftUI 从 TabView 内的 ForEach 中删除项目?
- google-chrome - Blink/Webkit Bug same domain different IPs
- angular - Problem with filtering after canceling edits in Angular Template Driven Form project
- html - How style Mozilla firefox vertical and horizontal scroll bars
- macos - Issue installing MP4Box: "image not found Abort trap: 6"