首页 > 解决方案 > 如何在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']”

标签: pythonkey-value

解决方案


谢谢大家。你为我指明了正确的方向。我可以通过以下方式实现我的目标:

for testname, value in checkvalue.test_results.items():
    for i in checkvalue.test_results[testname][0]['failed']:
        print(i['message'])

推荐阅读