1

我有一个名为 filelist = [] 的列表,我在其中附加了一些键值对的字典。

filelist.append({"url": url, "exename": ename, "filename": fname}] as I loop through a set of files.
Later, loads() the below json object to a "json" key: 

for file in filelist: file["json"] = json.loads(json)

我可以毫不费力地拉出第一层字典值,即。

for files in filelist:
    print files["json"]["response_code"]

>>> 1

但是,我在获取内部字典时遇到问题,我假设它绑定到 files["json"]["scans"] 例如:

for files in filelist:
    for avvendors in files["json"]["scans"]

我真的对提取“检测到”键很感兴趣,但似乎python没有将“扫描”键作为字典加载,而是将其存储为字符串。我正在考虑在内部字典上做第二次加载(),看看会发生什么。

for stuff in avvendors:
    if stuf["detected"]:
        #do something

我不太确定这里发生了什么。我知道我可能会弄明白,但是深入了解一下 python 在内部对 json 做了什么会很好。另外,直到星期一或星期二,甚至可能是星期三,我才有机会在这方面工作,而悬念正在扼杀我。

这是可读性的完整块:

for files in filelist: for avvendors in files["json"]["scans]: if avvendors["detected"]: #做一些有用的事情

{"response_code": 1,
"verbose_msg": "Scan finished, scan information embedded in this object",

"resource": "99017f6eebbac24f351415dd410d522d",
"scan_id": "52d3df0ed60c46f336c131bf2ca454f73bafdc4b04dfa2aea80746f5ba9e6d1c-1273894724",
"md5": "99017f6eebbac24f351415dd410d522d",
"sha1": "4d1740485713a2ab3a4f5822a01f645fe8387f92",
"sha256": "52d3df0ed60c46f336c131bf2ca454f73bafdc4b04dfa2aea80746f5ba9e6d1c",

"scan_date": "2010-05-15 03:38:44",

"positives": 40,
"total": 40,
"scans": {"nProtect": {"detected": true, "version": "2010-05-14.01", "result": "Trojan.Generic.3611249", "update": "20100514"},
"CAT-QuickHeal": {"detected": true, "version": "10.00", "result": "Trojan.VB.acgy", "update": "20100514"},
"McAfee": {"detected": true, "version": "5.400.0.1158", "result": "Generic.dx!rkx", "update": "20100515"},
"TheHacker": {"detected": true, "version": "6.5.2.0.280", "result": "Trojan/VB.gen", "update": "20100514"},
"VirusBuster": {"detected": true, "version": "5.0.27.0", "result": "Trojan.VB.JFDE", "update": "20100514"},
"NOD32": {"detected": true, "version": "5115", "result": "a variant of Win32/Qhost.NTY", "update": "20100514"},
"F-Prot": {"detected": false, "version": "4.5.1.85", "result": null, "update": "20100514"},
"Symantec": {"detected": true, "version": "20101.1.0.89", "result": "Trojan.KillAV", "update": "20100515"},
"Norman": {"detected": true, "version": "6.04.12", "result": "W32/Smalltroj.YFHZ", "update": "20100514"},
"TrendMicro-HouseCall": {"detected": true, "version": "9.120.0.1004", "result": "TROJ_VB.JVJ", "update": "20100515"},
"Avast": {"detected": true, "version": "4.8.1351.0", "result": "Win32:Malware-gen", "update": "20100514"},
"eSafe": {"detected": true, "version": "7.0.17.0", "result": "Win32.TRVB.Acgy", "update": "20100513"},
"ClamAV": {"detected": false, "version": "0.96.0.3-git", "result": null, "update": "20100514"},
"Kaspersky": {"detected": true, "version": "7.0.0.125", "result": "Trojan.Win32.VB.acgy", "update": "20100515"},
"BitDefender": {"detected": true, "version": "7.2", "result": "Trojan.Generic.3611249", "update": "20100515"},
"Comodo": {"detected": true, "version": "4842", "result": "Heur.Suspicious", "update": "20100515"},
"F-Secure": {"detected": true, "version": "9.0.15370.0", "result": "Trojan.Generic.3611249", "update": "20100514"},
"DrWeb": {"detected": true, "version": "5.0.2.03300", "result": "Trojan.Hosts.37", "update": "20100515"},
"AntiVir": {"detected": true, "version": "8.2.1.242", "result": "TR/VB.acgy.1", "update": "20100514"},
"TrendMicro": {"detected": true, "version": "9.120.0.1004", "result": "TROJ_VB.JVJ", "update": "20100514"},
"McAfee-GW-Edition": {"detected": true, "version": "2010.1", "result": "Generic.dx!rkx", "update": "20100515"},
"Sophos": {"detected": true, "version": "4.53.0", "result": "Troj/VBHost-A", "update": "20100515"},
"eTrust-Vet": {"detected": true, "version": "35.2.7490", "result": "Win32/ASuspect.HDBBD", "update": "20100515"},
"Authentium": {"detected": false, "version": "5.2.0.5", "result": null, "update": "20100514"},
"Jiangmin": {"detected": true, "version": "13.0.900", "result": "Trojan/VB.yqh", "update": "20100514"}, [...] }
4

1 回答 1

2

迭代字典时要小心。它只是返回键。如果您需要键和值,请使用以下.iteritems()方法(或在 Python3 中,.items()):

for files in filelist:
   for avvendor, stuf in files["json"]["scans"].iteritems():
       if stuf["detected"]:
          #do something

在您的情况下,scans键的值是一个字典:"scans": {"nProtect": ...}

于 2012-04-14T18:39:18.550 回答