我正在学习python。我遇到了一个无法克服的json问题。
我有这个数据集从json在python:
{
"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"
}
}
我想以这样一种方式过滤这些字典,即只打印"detected": true的键和值。.
例如,我想只打印
{
"Sophos": {
"detected": true,
"result": "phishing site"
},
"Fortinet": {
"detected": true,
"result": "phishing site"
}
}
我使用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)
我正在寻找一种简单易读的方式来做到这一点,这样我就能尽可能地理解它。我想用Python打印结果。我为此搜索并阅读了各种解决方案(列表推导或filter()与lambda),但它没有帮助我。
我还在学习中,如果这是一个简单的案例,请提前感谢您的理解。
提前感谢您的帮助和解答。
您可以使用字典推导来过滤响应字典。请注意,在您提供的示例中,我认为您有json数据而不是python对象。true
在python中不是一个有效的布尔关键字,它应该是True
。
filtered = {k: v for k, v in orignal_dict.items() if v.get("detected") == true}
对于你的例子-
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))
输出:
{
"Sophos": {
"detected": true,
"result": "phishing site"
},
"Fortinet": {
"detected": true,
"result": "phishing site"
},
"alphaMountain.ai": {
"detected": true,
"result": "phishing site"
}
}
你可以运行一个循环,然后放一个检查符
for key, value in example.items():
if value["detected"] == True:
print(key, value)
也可以通过filter()
函数实现。
list(filter(lambda x: x["detected"] == True, example.values()))
输出:
[{'detected': True, 'result': 'phishing site'}, {'detected': True, 'result': 'phishing site'}, {'detected': True, 'result': 'phishing site'}]
方法可以是:
for i in jsondump:
if jsondump[i]['detected'] == True:
print(jsondump[i])
就像我们在Jsondump中使用for each一样,它将导致给出所有保存数据的对象名称,即
for i in jsondump:
print(i)
上面的代码将导致:
Sophos
Phishtank
CyberCrime
..
..
Fortinet
alphaMountain.ai
Lionic
Cyble
现在,如果我们有键,我们可以使用jsondump[i]循环,值存储在'detected'中,所以我们将传入jsondump[i]['detected']来检查它是否为真。
这里我们可以使用字典推导式
new_dict = { key:val for (key,val) in example.items() if(value["detected"] == True)}
将创建一个新列表,其中所检测到的条件应为真
我们遍历整个字典然后对于每个元素也是一个字典(这里是val)我们检查检测到的值是否为True然后我们只将整个字典添加到new_dict
可以使用泛型函数按值过滤Python字典使用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))
输出:
{'Sophos': {'detected': True, 'result': 'phishing site'}, 'Fortinet': {'detected': True, 'result': 'phishing site'}, 'alphaMountain.ai': {'detected': True, 'result': 'phishing site'}}