简体   繁体   English

如何在python中过滤和打印特定的json字典

[英]How to filter and print particular json dictionaries in python

I'm in the process of learning python. I encountered a problem with json that I can't overcome.我在学习python的过程中遇到了一个json的问题无法攻克。

I have this dataset from json in python:我在 python 中有来自 json 的数据集:

{
    "Sophos": {
        "detected": true,
        "result": "phishing site"
    },
    "Phishtank": {
        "detected": false,
        "result": "clean site"
    },
    "CyberCrime": {
        "detected": false,
        "result": "clean site"
    },
    "Spam404": {
        "detected": false,
        "result": "clean site"
    },
    "SecureBrain": {
        "detected": false,
        "result": "clean site"
    },
    "Hoplite Industries": {
        "detected": false,
        "result": "clean site"
    },
    "CRDF": {
        "detected": false,
        "result": "clean site"
    },
    "Rising": {
        "detected": false,
        "result": "clean site"
    },
    "Fortinet": {
        "detected": true,
        "result": "phishing site"
    },
    "alphaMountain.ai": {
        "detected": true,
        "result": "phishing site"
    },
    "Lionic": {
        "detected": false,
        "result": "clean site"
    },
    "Cyble": {
        "detected": false,
        "result": "clean site"
    }
}

I would like to filter these dictionaries in such a way as to print only those keys and values in which "detected": true .我想以仅打印"detected": true的那些键和值的方式过滤这些字典。

For example I would like print only例如我只想打印

{
    "Sophos": {
        "detected": true,
        "result": "phishing site"
    },
    "Fortinet": {
        "detected": true,
        "result": "phishing site"
    }
}

I use VirusTotal apikey v2 https://developers.virustotal.com/v2.0/reference/domain-report My code in python:我使用 VirusTotal apikey v2 https://developers.virustotal.com/v2.0/reference/domain-report我在 python 中的代码:

parameters = {'apikey': api_key, 'resource': domain}

response = requests.get(url, params=parameters)
    
python_response = json.loads(response.text)

scans = python_response["scans"]

example = json.dumps(python_response["scans"], indent=4)

print(example)

I'm looking for a simple and readable way to do it so that I understand it as best I can.我正在寻找一种简单易读的方法来做到这一点,以便我尽可能地理解它。 I would like print result in Python. I searched and read various solutions for this (list comprehension or filter() with lambda), but it did not help me.我想要 Python 中的打印结果。我为此搜索并阅读了各种解决方案(列表理解或 filter() with lambda),但它对我没有帮助。

I'm still learning, thanks in advance for your understanding if it's a simple case.我还在学习,如果这是一个简单的案例,请提前感谢您的理解。

Thank you in advance for your help and answers.预先感谢您的帮助和回答。

You can use dict comprehension to filter the response dictionary.您可以使用字典理解来过滤响应字典。 Note that in the example you provided, I think you have json data and not the python object. true is not a valid boolean keyword in python, it should've been True instead.请注意,在您提供的示例中,我认为您有 json 数据而不是 python true不是 python 中有效的 boolean 关键字,它应该是True

filtered = {k: v for k, v in orignal_dict.items() if v.get("detected") == true}

For your example -对于你的例子 -

true = True
false = False

data = {
    "Sophos": {
        "detected": true,
        "result": "phishing site"
    },
    "Phishtank": {
        "detected": false,
        "result": "clean site"
    },
    "CyberCrime": {
        "detected": false,
        "result": "clean site"
    },
    "Spam404": {
        "detected": false,
        "result": "clean site"
    },
    "SecureBrain": {
        "detected": false,
        "result": "clean site"
    },
    "Hoplite Industries": {
        "detected": false,
        "result": "clean site"
    },
    "CRDF": {
        "detected": false,
        "result": "clean site"
    },
    "Rising": {
        "detected": false,
        "result": "clean site"
    },
    "Fortinet": {
        "detected": true,
        "result": "phishing site"
    },
    "alphaMountain.ai": {
        "detected": true,
        "result": "phishing site"
    },
    "Lionic": {
        "detected": false,
        "result": "clean site"
    },
    "Cyble": {
        "detected": false,
        "result": "clean site"
    }
}


filtered = {k: v for k, v in data.items() if v.get("detected") == true}
print(json.dumps(filtered, indent=4))

Output: Output:

{
    "Sophos": {
        "detected": true,
        "result": "phishing site"
    },
    "Fortinet": {
        "detected": true,
        "result": "phishing site"
    },
    "alphaMountain.ai": {
        "detected": true,
        "result": "phishing site"
    }
}

You can run a loop and then put a check您可以运行一个循环,然后进行检查

for key, value in example.items():
    if value["detected"] == True:
        print(key, value)

Alternatively, You can use the filter() function to achieve it.或者,您可以使用filter() function 来实现它。

list(filter(lambda x: x["detected"] == True, example.values()))

Output: Output:

[{'detected': True, 'result': 'phishing site'}, {'detected': True, 'result': 'phishing site'}, {'detected': True, 'result': 'phishing site'}]

An approach can be:一种方法可以是:

for i in jsondump:
    if jsondump[i]['detected'] == True:
        print(jsondump[i])

As if we loop in Jsondump using for each, it would result in giving all object names that hold data ie就好像我们在 Jsondump 中循环使用 for each 一样,它会导致给出所有保存数据的 object 名称,即

for i in jsondump:
     print(i)

The above code will result in:上面的代码将导致:

Sophos  
Phishtank  
CyberCrime  
..  
..  
Fortinet  
alphaMountain.ai
Lionic  
Cyble  

Now if we have the keys, we can loop in using jsondump[i] and the value is stored in 'detected', so we will pass in jsondump[i]['detected'] to check if it is true.现在如果我们有密钥,我们可以使用 jsondump[i] 循环并将值存储在 'detected' 中,因此我们将传入 jsondump[i]['detected'] 以检查它是否为真。

Over here we can use the dictionary comprehension在这里我们可以使用字典理解

new_dict = { key:val for (key,val) in example.items() if(value["detected"] == True)}

a new list would be created with your desired condition that detected should be true将根据您想要的条件创建一个新列表,检测到的条件应该为真

over here we iterate over the entire dictionary and then for each of the elements which is also a dictionary (val in this case ) we are checking if the value of the detected is True then only we add the entire dictionary to the new_dict在这里,我们遍历整个字典,然后对于也是字典的每个元素(在本例中为 val),我们检查检测到的值是否为 True 然后我们只将整个字典添加到 new_dict

You could use filter Python dictionary by value Using Generic Function with lambda, check the following example:您可以按值使用过滤器 Python 字典使用通用 Function和 lambda,检查以下示例:

dataset = {
    "Sophos": {
        "detected": True,
        "result": "phishing site"
    },
    "Phishtank": {
        "detected": False,
        "result": "clean site"
    },
    "CyberCrime": {
        "detected": False,
        "result": "clean site"
    },
    "Spam404": {
        "detected": False,
        "result": "clean site"
    },
    "SecureBrain": {
        "detected": False,
        "result": "clean site"
    },
    "Hoplite Industries": {
        "detected": False,
        "result": "clean site"
    },
    "CRDF": {
        "detected": False,
        "result": "clean site"
    },
    "Rising": {
        "detected": False,
        "result": "clean site"
    },
    "Fortinet": {
        "detected": True,
        "result": "phishing site"
    },
    "alphaMountain.ai": {
        "detected": True,
        "result": "phishing site"
    },
    "Lionic": {
        "detected": False,
        "result": "clean site"
    },
    "Cyble": {
        "detected": False,
        "result": "clean site"
    }
}

def filter_dict(d, f):
    ''' Filters dictionary d by function f. '''
    newDict = dict()
    # Iterate over all (k,v) pairs in dict
    for key, value in d.items():
        # Is condition satisfied?
        if f(key, value):
            newDict[key] = value
    return newDict

print(filter_dict(dataset, lambda k, v: v['detected'] == True))

Output: Output:

{'Sophos': {'detected': True, 'result': 'phishing site'}, 'Fortinet': {'detected': True, 'result': 'phishing site'}, 'alphaMountain.ai': {'detected': True, 'result': 'phishing site'}}

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

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