简体   繁体   English

如何从python中的字典值获取特定条目

[英]How to get particular entry from value of dictionary in python

Here, this is value of my dictionary but I want to get only details like product and version of 443 and 80. Is there any way or command with the help of which, we can gethis info? 在这里,这是我的字典的价值,但是我只想获取产品和443和80版本之类的详细信息。有什么方法或命令可以帮助您获得其信息? Here is my dictionary value: 这是我的字典值:

   {'nmap': {'scanstats': {'timestr': 'Fri Apr 17 05:08:18 2015', 'uphosts': '1', 'downhosts': '0', 'totalhosts': '1', 'elapsed': '14.91'}, 'scaninfo': {'tcp': {'services': '80,443', 'method': 'connect'}}, 'command_line': 'nmap -oX - -p 80,443 -sV xxxx'}, 'scan': {'x.x.x.x': {'status': {'state': 'up', 'reason': 'syn-ack'}, 'hostname': 'xxxx', 'vendor': {}, 'addresses': {'ipv4': '0x.x.x'}, 'tcp': {'443': {'product': 'Apache Tomcat/Coyote JSP engine', 'name': 'http', 'extrainfo': '', 'reason': 'syn-ack', 'cpe': '', 'state': 'open', 'version': '1.1', 'conf': '10'}, '80': {'product': 'Apache Tomcat/Coyote JSP engine', 'name': 'http', 'extrainfo': '', 'reason': 'syn-ack', 'cpe': '', 'state': 'open', 'version': '1.1', 'conf': '0'}}}}}

So. 所以。 I ran this command 我跑了这个命令

      scan=[v for k,v in x.iteritems() if 'scan' in k]

It gives me result below: 它给我以下结果:

    [{
    'x.x.x.x': {
        'status': {
            'state': 'up',
            'reason': 'syn-ack'
        },
        'hostname': 'xxxx',
        'vendor': {},
        'addresses': {
            'ipv4': 'x.x.x.x'
        },
        'tcp': {
            '443': {
                'product': 'Apache Tomcat/Coyote JSP engine',
                'name': 'http',
                'extrainfo': '',
                'reason': 'syn-ack',
                'cpe': '',
                'state': 'open',
                'version': '1.1',
                'conf': '10'
            },
            '80': {
                'product': '',
                'name': 'http',
                'extrainfo': '',
                'reason': 'conn-refused',
                'cpe': '',
                'state': 'closed',
                'version': '',
                'conf': '3'
            }
        }
    }
}]

You can try the following: 您可以尝试以下方法:

>>> data = [{'x.x.x.x': {'status': {'state': 'up', 'reason': 'syn-ack'}, 'hostname': 'xxxx', 'vendor': {}, 'addresses': {'ipv4': 'x.x.x.x'}, 'tcp': {'443': {'product': 'Apache Tomcat/Coyote JSP engine', 'name': 'http', 'extrainfo': '', 'reason': 'syn-ack', 'cpe': '', 'state': 'open', 'version': '1.1', 'conf': '10'}, '80': {'product': '', 'name': 'http', 'extrainfo': '', 'reason': 'conn-refused', 'cpe': '', 'state': 'closed', 'version': '', 'conf': '3'}}}}]
>>> for i in data[0]['x.x.x.x']['tcp']:
...     print i, data[0]['x.x.x.x']['tcp'][i]['product'], data[0]['x.x.x.x']['tcp'][i]['version']
... 
443 Apache Tomcat/Coyote JSP engine 1.1
80  
>>> 

You could use method items ( iteritems in Python 2) for extracting both port number and associated information: 您可以使用方法items (Python 2中的iteritems items )来提取端口号和相关信息:

In [4]: for port, info in data[0]['x.x.x.x']['tcp'].items():
   ...:     print(port, info['product'], info['version'])
   ...:   
443 Apache Tomcat/Coyote JSP engine 1.1
80  

You can always use d.keys() to see what is in the dictionary keys to traverse it. 您始终可以使用d.keys()来查看字典键中的内容以进行遍历。

    d = your dictionary

    d['x.x.x.x']['tcp']['443']['product']
    Out[109]: 'Apache Tomcat/Coyote JSP engine'

    d['x.x.x.x']['tcp']['443']['version']
    Out[110]: '1.1'

    d['x.x.x.x']['tcp']['80']['product']
    Out[109]: ''

    d['x.x.x.x']['tcp']['80']['version']
    Out[110]: ''

Your data is basically a tree, so it can be traversed recursively with a function like this: 您的数据基本上是一棵树,因此可以使用以下函数递归遍历它:

def parse_data(output, keys_wanted, values_wanted, data):
    for key, val in data.iteritems():
        if key in keys_wanted:
            output.update({key: {k: val[k] for k in values_wanted}})
        if isinstance(val, dict):
            parse_data(output, keys_wanted, values_wanted, val)

Use: 采用:

data = <your dict>
keys = ['443', '80']
vals = ['product', 'version']
out = {}
parse_data(out, keys, vals, data)

Output: 输出:

>>> print out
{'443': {'product': 'Apache Tomcat/Coyote JSP engine', 'version': '1.1'}, '80': {'product': '', 'version': ''}}

A benefit to this function is that it's general purpose -- if you want different keys and values just pass different lists in the parameters. 此功能的一个好处是它是通用的-如果您想要不同的键和值,只需在参数中传递不同的列表即可。

BTW, in your sample input, the dict is inside a list, but there's just one item, so I stripped off the list brackets for simplicity's sake. 顺便说一句,在您的示例输入中,字典位于列表中,但是只有一项,因此为了简单起见,我删除了列表括号。 If your actual data is in a list with many other items, you'd of course want to call this function in an iteration loop. 如果您的实际数据在包含许多其他项的列表中,那么您当然希望在迭代循环中调用此函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM