如何使用Python3读取和组合多个JSON文件?

时间:2017-06-22 16:35:47

标签: python json python-3.x csv

我在文件夹中有多个JSON文件,结构如下:

实际文件1:

{"scans": {"Bkav": {"detected": false, "version": "1.3.0.8876", "result": null, "update": "20170613"}, "TotalDefense": {"detected": false, "version": "37.1.62.1", "result": null, "update": "20170613"}, "MicroWorld-eScan": {"detected": false, "version": "12.0.250.0", "result": null, "update": "20170613"}, "nProtect": {"detected": false, "version": "2017-06-13.02", "result": null, "update": "20170613"}, "CMC": {"detected": false, "version": "1.1.0.977", "result": null, "update": "20170613"}, "CAT-QuickHeal": {"detected": false, "version": "14.00", "result": null, "update": "20170613"}, "McAfee": {"detected": false, "version": "6.0.6.653", "result": null, "update": "20170613"}, "Malwarebytes": {"detected": false, "version": "2.1.1.1115", "result": null, "update": "20170613"}, "Zillya": {"detected": false, "version": "2.0.0.3311", "result": null, "update": "20170613"}, "SUPERAntiSpyware": {"detected": false, "version": "5.6.0.1032", "result": null, "update": "20170613"}, "TheHacker": {"detected": false, "version": "6.8.0.5.1623", "result": null, "update": "20170612"}, "K7GW": {"detected": false, "version": "10.15.23651", "result": null, "update": "20170613"}, "K7AntiVirus": {"detected": false, "version": "10.15.23640", "result": null, "update": "20170613"}, "Arcabit": {"detected": false, "version": "1.0.0.806", "result": null, "update": "20170613"}, "Baidu": {"detected": false, "version": "1.0.0.2", "result": null, "update": "20170613"}, "F-Prot": {"detected": false, "version": "4.7.1.166", "result": null, "update": "20170613"}, "Symantec": {"detected": false, "version": "1.3.1.0", "result": null, "update": "20170613"}, "ESET-NOD32": {"detected": false, "version": "15577", "result": null, "update": "20170613"}, "TrendMicro-HouseCall": {"detected": false, "version": "9.900.0.1004", "result": null, "update": "20170613"}, "Avast": {"detected": false, "version": "8.0.1489.320", "result": null, "update": "20170613"}, "ClamAV": {"detected": false, "version": "0.99.2.0", "result": null, "update": "20170613"}, "Kaspersky": {"detected": false, "version": "15.0.1.13", "result": null, "update": "20170613"}, "BitDefender": {"detected": false, "version": "7.2", "result": null, "update": "20170613"}, "NANO-Antivirus": {"detected": false, "version": "1.0.76.17389", "result": null, "update": "20170613"}, "Paloalto": {"detected": false, "version": "1.0", "result": null, "update": "20170613"}, "ViRobot": {"detected": false, "version": "2014.3.20.0", "result": null, "update": "20170613"}, "Tencent": {"detected": false, "version": "1.0.0.1", "result": null, "update": "20170613"}, "Ad-Aware": {"detected": false, "version": "3.0.3.1010", "result": null, "update": "20170613"}, "Emsisoft": {"detected": false, "version": "4.0.1.883", "result": null, "update": "20170613"}, "Comodo": {"detected": false, "version": "27271", "result": null, "update": "20170613"}, "F-Secure": {"detected": false, "version": "11.0.19100.45", "result": null, "update": "20170613"}, "DrWeb": {"detected": false, "version": "7.0.28.2020", "result": null, "update": "20170613"}, "VIPRE": {"detected": false, "version": "58800", "result": null, "update": "20170613"}, "Invincea": {"detected": false, "version": "6.3.0.25415", "result": null, "update": "20170607"}, "McAfee-GW-Edition": {"detected": false, "version": "v2015", "result": null, "update": "20170613"}, "Sophos": {"detected": false, "version": "4.98.0", "result": null, "update": "20170613"}, "Ikarus": {"detected": false, "version": "0.1.5.2", "result": null, "update": "20170613"}, "Cyren": {"detected": false, "version": "5.4.30.7", "result": null, "update": "20170613"}, "Jiangmin": {"detected": false, "version": "16.0.100", "result": null, "update": "20170613"}, "Webroot": {"detected": false, "version": "1.0.0.207", "result": null, "update": "20170613"}, "Avira": {"detected": false, "version": "8.3.3.4", "result": null, "update": "20170613"}, "Kingsoft": {"detected": false, "version": "2013.8.14.323", "result": null, "update": "20170613"}, "Endgame": {"detected": false, "version": "0.7.0", "result": null, "update": "20170612"}, "Microsoft": {"detected": false, "version": "1.1.13804.0", "result": null, "update": "20170613"}, "AegisLab": {"detected": false, "version": "4.2", "result": null, "update": "20170613"}, "ZoneAlarm": {"detected": false, "version": "1.0", "result": null, "update": "20170613"}, "GData": {"detected": false, "version": "A:25.12848B:25.9761", "result": null, "update": "20170613"}, "AhnLab-V3": {"detected": false, "version": "3.9.1.17781", "result": null, "update": "20170613"}, "ALYac": {"detected": false, "version": "1.0.1.9", "result": null, "update": "20170613"}, "AVware": {"detected": false, "version": "1.5.0.42", "result": null, "update": "20170613"}, "VBA32": {"detected": false, "version": "3.12.26.4", "result": null, "update": "20170613"}, "Zoner": {"detected": false, "version": "1.0", "result": null, "update": "20170613"}, "Rising": {"detected": false, "version": "28.0.0.1", "result": null, "update": "20170613"}, "Yandex": {"detected": false, "version": "5.5.1.3", "result": null, "update": "20170608"}, "SentinelOne": {"detected": false, "version": "1.0.0.12", "result": null, "update": "20170516"}, "Fortinet": {"detected": false, "version": "5.4.233.0", "result": null, "update": "20170613"}, "AVG": {"detected": false, "version": "8.0.1489.320", "result": null, "update": "20170613"}, "Panda": {"detected": false, "version": "4.6.4.2", "result": null, "update": "20170613"}, "CrowdStrike": {"detected": false, "version": "1.0", "result": null, "update": "20170420"}, "Qihoo-360": {"detected": false, "version": "1.0.0.1120", "result": null, "update": "20170613"}}, "scan_id": "00d9d7d8e563ae71dcecc808f35f7d0845ffd91a1731d3f69e6ea5204fd7a3d7-1497385194", "sha1": "c6a6e3977402e76379f48f09a052f0f3c50f5964", "resource": "00D9D7D8E563AE71DCECC808F35F7D0845FFD91A1731D3F69E6EA5204FD7A3D7", "response_code": 1, "scan_date": "2017-06-13 20:19:54", "permalink": "https://www.virustotal.com/file/00d9d7d8e563ae71dcecc808f35f7d0845ffd91a1731d3f69e6ea5204fd7a3d7/analysis/1497385194/", "verbose_msg": "Scan finished, information embedded", "total": 60, "positives": 0, "sha256": "00d9d7d8e563ae71dcecc808f35f7d0845ffd91a1731d3f69e6ea5204fd7a3d7", "md5": "8d95236c637c042ff7df7fd7cc502ddb"}

实际文件2:

{"scans": {"MicroWorld-eScan": {"detected": false, "version": "12.0.250.0", "result": null, "update": "20170610"}, "nProtect": {"detected": false, "version": "2017-06-10.02", "result": null, "update": "20170610"}, "CMC": {"detected": false, "version": "1.1.0.977", "result": null, "update": "20170610"}, "CAT-QuickHeal": {"detected": true, "version": "14.00", "result": "TrojDownloader.NSIS.Genome.V", "update": "20170610"}, "ALYac": {"detected": false, "version": "1.0.1.9", "result": null, "update": "20170610"}, "Malwarebytes": {"detected": true, "version": "2.1.1.1115", "result": "PUP.Optional.MyPCBackup", "update": "20170610"}, "Zillya": {"detected": false, "version": "2.0.0.3308", "result": null, "update": "20170610"}, "AegisLab": {"detected": false, "version": "4.2", "result": null, "update": "20170610"}, "TheHacker": {"detected": false, "version": "6.8.0.5.1596", "result": null, "update": "20170607"}, "K7GW": {"detected": false, "version": "10.14.23624", "result": null, "update": "20170610"}, "K7AntiVirus": {"detected": false, "version": "10.14.23624", "result": null, "update": "20170610"}, "Arcabit": {"detected": false, "version": "1.0.0.806", "result": null, "update": "20170610"}, "TrendMicro": {"detected": false, "version": "9.740.0.1012", "result": null, "update": "20170610"}, "Baidu": {"detected": true, "version": "1.0.0.2", "result": "Win32.Trojan.WisdomEyes.16070401.9500.9976", "update": "20170608"}, "F-Prot": {"detected": false, "version": "4.7.1.166", "result": null, "update": "20170610"}, "Symantec": {"detected": true, "version": "1.3.1.0", "result": "PUA.MyPCBackup", "update": "20170610"}, "TotalDefense": {"detected": false, "version": "37.1.62.1", "result": null, "update": "20170610"}, "TrendMicro-HouseCall": {"detected": false, "version": "9.900.0.1004", "result": null, "update": "20170610"}, "Paloalto": {"detected": false, "version": "1.0", "result": null, "update": "20170610"}, "ClamAV": {"detected": false, "version": "0.99.2.0", "result": null, "update": "20170610"}, "Kaspersky": {"detected": false, "version": "15.0.1.13", "result": null, "update": "20170610"}, "BitDefender": {"detected": false, "version": "7.2", "result": null, "update": "20170610"}, "NANO-Antivirus": {"detected": true, "version": "1.0.76.17389", "result": "Riskware.Win32.Unwanted.dmgktv", "update": "20170610"}, "SUPERAntiSpyware": {"detected": false, "version": "5.6.0.1032", "result": null, "update": "20170610"}, "Avast": {"detected": false, "version": "8.0.1489.320", "result": null, "update": "20170610"}, "Tencent": {"detected": false, "version": "1.0.0.1", "result": null, "update": "20170610"}, "Ad-Aware": {"detected": false, "version": "3.0.3.1010", "result": null, "update": "20170610"}, "Emsisoft": {"detected": false, "version": "4.0.1.883", "result": null, "update": "20170610"}, "Comodo": {"detected": false, "version": "27254", "result": null, "update": "20170610"}, "F-Secure": {"detected": false, "version": "11.0.19100.45", "result": null, "update": "20170610"}, "DrWeb": {"detected": true, "version": "7.0.28.2020", "result": "Program.Unwanted.567", "update": "20170610"}, "VIPRE": {"detected": false, "version": "58730", "result": null, "update": "20170610"}, "Invincea": {"detected": false, "version": "6.3.0.25415", "result": null, "update": "20170607"}, "McAfee-GW-Edition": {"detected": false, "version": "v2015", "result": null, "update": "20170610"}, "Sophos": {"detected": false, "version": "4.98.0", "result": null, "update": "20170610"}, "Ikarus": {"detected": false, "version": "0.1.5.2", "result": null, "update": "20170610"}, "Cyren": {"detected": false, "version": "5.4.30.7", "result": null, "update": "20170610"}, "Jiangmin": {"detected": false, "version": "16.0.100", "result": null, "update": "20170610"}, "Webroot": {"detected": false, "version": "1.0.0.207", "result": null, "update": "20170610"}, "Avira": {"detected": true, "version": "8.3.3.4", "result": "PUA/MyPCBackup.Gen", "update": "20170610"}, "Kingsoft": {"detected": false, "version": "2013.8.14.323", "result": null, "update": "20170610"}, "Endgame": {"detected": false, "version": "0.5.0", "result": null, "update": "20170515"}, "Microsoft": {"detected": false, "version": "1.1.13804.0", "result": null, "update": "20170610"}, "ViRobot": {"detected": false, "version": "2014.3.20.0", "result": null, "update": "20170610"}, "ZoneAlarm": {"detected": false, "version": "1.0", "result": null, "update": "20170610"}, "GData": {"detected": true, "version": "A:25.12800B:25.9740", "result": "NSIS.Adware.MyPCBackup.E", "update": "20170610"}, "AhnLab-V3": {"detected": false, "version": "3.9.0.17697", "result": null, "update": "20170610"}, "McAfee": {"detected": false, "version": "6.0.6.653", "result": null, "update": "20170610"}, "AVware": {"detected": false, "version": "1.5.0.42", "result": null, "update": "20170610"}, "VBA32": {"detected": false, "version": "3.12.26.4", "result": null, "update": "20170609"}, "Zoner": {"detected": false, "version": "1.0", "result": null, "update": "20170610"}, "ESET-NOD32": {"detected": true, "version": "15562", "result": "MSIL/MyPCBackup.D potentially unwanted", "update": "20170610"}, "Rising": {"detected": true, "version": "28.0.0.1", "result": "Malware.Undefined!8.C (cloud:I1YBt1VpobT) ", "update": "20170610"}, "Yandex": {"detected": true, "version": "5.5.1.3", "result": "Riskware.Agent!", "update": "20170608"}, "SentinelOne": {"detected": false, "version": "1.0.0.12", "result": null, "update": "20170516"}, "Fortinet": {"detected": false, "version": "5.4.233.0", "result": null, "update": "20170610"}, "AVG": {"detected": false, "version": "8.0.1489.320", "result": null, "update": "20170610"}, "Panda": {"detected": false, "version": "4.6.4.2", "result": null, "update": "20170610"}, "CrowdStrike": {"detected": false, "version": "1.0", "result": null, "update": "20170420"}, "Qihoo-360": {"detected": false, "version": "1.0.0.1120", "result": null, "update": "20170610"}}, "scan_id": "00d468fa26813736cd14ff91e84f5e31fe30eaef6b35af44cafe540870ea7873-1497129945", "sha1": "7b890323abfe8f3bd33be0bc439076b5525d03b0", "resource": "00D468FA26813736CD14FF91E84F5E31FE30EAEF6B35AF44CAFE540870EA7873", "response_code": 1, "scan_date": "2017-06-10 21:25:45", "permalink": "https://www.virustotal.com/file/00d468fa26813736cd14ff91e84f5e31fe30eaef6b35af44cafe540870ea7873/analysis/1497129945/", "verbose_msg": "Scan finished, information embedded", "total": 60, "positives": 11, "sha256": "00d468fa26813736cd14ff91e84f5e31fe30eaef6b35af44cafe540870ea7873", "md5": "45922155c9628e11441aa869c6287bb7"}

实际文件3:

{"response_code": 0, "resource": "0E28BEDFBA37CEE5BD639AC86AC08A422C8944C3749CD2C5D7F5A0C2B37115B3", "verbose_msg": "The requested resource is not among the finished, queued or pending scans"}

注意当response_code为'0'时文件3是如何不同的。

我们需要将这些JSON文件合并到一个Python对象中,例如一本字典,理想情况下可以转换成CSV文件。

CSV文件如下所示:

scans/Bkav/detected,scans/Bkav/version,scans/Bkav/result,scans/Bkav/update,scans/TotalDefense/detected,scans/TotalDefense/version,scans/TotalDefense/result,scans/TotalDefense/update,scans/MicroWorld-eScan/detected,scans/MicroWorld-eScan/version,scans/MicroWorld-eScan/result,scans/MicroWorld-eScan/update,scans/nProtect/detected,scans/nProtect/version,scans/nProtect/result,scans/nProtect/update,scans/CMC/detected,scans/CMC/version,scans/CMC/result,scans/CMC/update,scans/CAT-QuickHeal/detected,scans/CAT-QuickHeal/version,scans/CAT-QuickHeal/result,scans/CAT-QuickHeal/update,scans/McAfee/detected,scans/McAfee/version,scans/McAfee/result,scans/McAfee/update,scans/Malwarebytes/detected,scans/Malwarebytes/version,scans/Malwarebytes/result,scans/Malwarebytes/update,scans/Zillya/detected,scans/Zillya/version,scans/Zillya/result,scans/Zillya/update,scans/SUPERAntiSpyware/detected,scans/SUPERAntiSpyware/version,scans/SUPERAntiSpyware/result,scans/SUPERAntiSpyware/update,scans/TheHacker/detected,scans/TheHacker/version,scans/TheHacker/result,scans/TheHacker/update,scans/K7GW/detected,scans/K7GW/version,scans/K7GW/result,scans/K7GW/update,scans/K7AntiVirus/detected,scans/K7AntiVirus/version,scans/K7AntiVirus/result,scans/K7AntiVirus/update,scans/Arcabit/detected,scans/Arcabit/version,scans/Arcabit/result,scans/Arcabit/update,scans/Baidu/detected,scans/Baidu/version,scans/Baidu/result,scans/Baidu/update,scans/F-Prot/detected,scans/F-Prot/version,scans/F-Prot/result,scans/F-Prot/update,scans/Symantec/detected,scans/Symantec/version,scans/Symantec/result,scans/Symantec/update,scans/ESET-NOD32/detected,scans/ESET-NOD32/version,scans/ESET-NOD32/result,scans/ESET-NOD32/update,scans/TrendMicro-HouseCall/detected,scans/TrendMicro-HouseCall/version,scans/TrendMicro-HouseCall/result,scans/TrendMicro-HouseCall/update,scans/Avast/detected,scans/Avast/version,scans/Avast/result,scans/Avast/update,scans/ClamAV/detected,scans/ClamAV/version,scans/ClamAV/result,scans/ClamAV/update,scans/Kaspersky/detected,scans/Kaspersky/version,scans/Kaspersky/result,scans/Kaspersky/update,scans/BitDefender/detected,scans/BitDefender/version,scans/BitDefender/result,scans/BitDefender/update,scans/NANO-Antivirus/detected,scans/NANO-Antivirus/version,scans/NANO-Antivirus/result,scans/NANO-Antivirus/update,scans/Paloalto/detected,scans/Paloalto/version,scans/Paloalto/result,scans/Paloalto/update,scans/ViRobot/detected,scans/ViRobot/version,scans/ViRobot/result,scans/ViRobot/update,scans/Tencent/detected,scans/Tencent/version,scans/Tencent/result,scans/Tencent/update,scans/Ad-Aware/detected,scans/Ad-Aware/version,scans/Ad-Aware/result,scans/Ad-Aware/update,scans/Emsisoft/detected,scans/Emsisoft/version,scans/Emsisoft/result,scans/Emsisoft/update,scans/Comodo/detected,scans/Comodo/version,scans/Comodo/result,scans/Comodo/update,scans/F-Secure/detected,scans/F-Secure/version,scans/F-Secure/result,scans/F-Secure/update,scans/DrWeb/detected,scans/DrWeb/version,scans/DrWeb/result,scans/DrWeb/update,scans/VIPRE/detected,scans/VIPRE/version,scans/VIPRE/result,scans/VIPRE/update,scans/Invincea/detected,scans/Invincea/version,scans/Invincea/result,scans/Invincea/update,scans/McAfee-GW-Edition/detected,scans/McAfee-GW-Edition/version,scans/McAfee-GW-Edition/result,scans/McAfee-GW-Edition/update,scans/Sophos/detected,scans/Sophos/version,scans/Sophos/result,scans/Sophos/update,scans/Ikarus/detected,scans/Ikarus/version,scans/Ikarus/result,scans/Ikarus/update,scans/Cyren/detected,scans/Cyren/version,scans/Cyren/result,scans/Cyren/update,scans/Jiangmin/detected,scans/Jiangmin/version,scans/Jiangmin/result,scans/Jiangmin/update,scans/Webroot/detected,scans/Webroot/version,scans/Webroot/result,scans/Webroot/update,scans/Avira/detected,scans/Avira/version,scans/Avira/result,scans/Avira/update,scans/Kingsoft/detected,scans/Kingsoft/version,scans/Kingsoft/result,scans/Kingsoft/update,scans/Endgame/detected,scans/Endgame/version,scans/Endgame/result,scans/Endgame/update,scans/Microsoft/detected,scans/Microsoft/version,scans/Microsoft/result,scans/Microsoft/update,scans/AegisLab/detected,scans/AegisLab/version,scans/AegisLab/result,scans/AegisLab/update,scans/ZoneAlarm/detected,scans/ZoneAlarm/version,scans/ZoneAlarm/result,scans/ZoneAlarm/update,scans/GData/detected,scans/GData/version,scans/GData/result,scans/GData/update,scans/AhnLab-V3/detected,scans/AhnLab-V3/version,scans/AhnLab-V3/result,scans/AhnLab-V3/update,scans/ALYac/detected,scans/ALYac/version,scans/ALYac/result,scans/ALYac/update,scans/AVware/detected,scans/AVware/version,scans/AVware/result,scans/AVware/update,scans/VBA32/detected,scans/VBA32/version,scans/VBA32/result,scans/VBA32/update,scans/Zoner/detected,scans/Zoner/version,scans/Zoner/result,scans/Zoner/update,scans/Rising/detected,scans/Rising/version,scans/Rising/result,scans/Rising/update,scans/Yandex/detected,scans/Yandex/version,scans/Yandex/result,scans/Yandex/update,scans/SentinelOne/detected,scans/SentinelOne/version,scans/SentinelOne/result,scans/SentinelOne/update,scans/Fortinet/detected,scans/Fortinet/version,scans/Fortinet/result,scans/Fortinet/update,scans/AVG/detected,scans/AVG/version,scans/AVG/result,scans/AVG/update,scans/Panda/detected,scans/Panda/version,scans/Panda/result,scans/Panda/update,scans/CrowdStrike/detected,scans/CrowdStrike/version,scans/CrowdStrike/result,scans/CrowdStrike/update,scans/Qihoo-360/detected,scans/Qihoo-360/version,scans/Qihoo-360/result,scans/Qihoo-360/update,scan_id,sha1,resource,response_code,scan_date,permalink,verbose_msg,total,positives,sha256,md5,scans/TrendMicro/detected,scans/TrendMicro/version,scans/TrendMicro/result,scans/TrendMicro/update
false,1.3.0.8876,,20170613,false,37.1.62.1,,20170613,false,12.0.250.0,,20170613,false,2017-06-13.02,,20170613,false,1.1.0.977,,20170613,false,14.00,,20170613,false,6.0.6.653,,20170613,false,2.1.1.1115,,20170613,false,2.0.0.3311,,20170613,false,5.6.0.1032,,20170613,false,6.8.0.5.1623,,20170612,false,10.15.23651,,20170613,false,10.15.23640,,20170613,false,1.0.0.806,,20170613,false,1.0.0.2,,20170613,false,4.7.1.166,,20170613,false,1.3.1.0,,20170613,false,15577,,20170613,false,9.900.0.1004,,20170613,false,8.0.1489.320,,20170613,false,0.99.2.0,,20170613,false,15.0.1.13,,20170613,false,7.2,,20170613,false,1.0.76.17389,,20170613,false,1.0,,20170613,false,2014.3.20.0,,20170613,false,1.0.0.1,,20170613,false,3.0.3.1010,,20170613,false,4.0.1.883,,20170613,false,27271,,20170613,false,11.0.19100.45,,20170613,false,7.0.28.2020,,20170613,false,58800,,20170613,false,6.3.0.25415,,20170607,false,v2015,,20170613,false,4.98.0,,20170613,false,0.1.5.2,,20170613,false,5.4.30.7,,20170613,false,16.0.100,,20170613,false,1.0.0.207,,20170613,false,8.3.3.4,,20170613,false,2013.8.14.323,,20170613,false,0.7.0,,20170612,false,1.1.13804.0,,20170613,false,4.2,,20170613,false,1.0,,20170613,false,A:25.12848B:25.9761,,20170613,false,3.9.1.17781,,20170613,false,1.0.1.9,,20170613,false,1.5.0.42,,20170613,false,3.12.26.4,,20170613,false,1.0,,20170613,false,28.0.0.1,,20170613,false,5.5.1.3,,20170608,false,1.0.0.12,,20170516,false,5.4.233.0,,20170613,false,8.0.1489.320,,20170613,false,4.6.4.2,,20170613,false,1.0,,20170420,false,1.0.0.1120,,20170613,00d9d7d8e563ae71dcecc808f35f7d0845ffd91a1731d3f69e6ea5204fd7a3d7-1497385194,c6a6e3977402e76379f48f09a052f0f3c50f5964,00D9D7D8E563AE71DCECC808F35F7D0845FFD91A1731D3F69E6EA5204FD7A3D7,1,2017-06-13 20:19:54,https://www.virustotal.com/file/00d9d7d8e563ae71dcecc808f35f7d0845ffd91a1731d3f69e6ea5204fd7a3d7/analysis/1497385194/,"Scan finished, information embedded",60,0,00d9d7d8e563ae71dcecc808f35f7d0845ffd91a1731d3f69e6ea5204fd7a3d7,8d95236c637c042ff7df7fd7cc502ddb,,,,
,,,,false,37.1.62.1,,20170610,false,12.0.250.0,,20170610,false,2017-06-10.02,,20170610,false,1.1.0.977,,20170610,true,14.00,TrojDownloader.NSIS.Genome.V,20170610,false,6.0.6.653,,20170610,true,2.1.1.1115,PUP.Optional.MyPCBackup,20170610,false,2.0.0.3308,,20170610,false,5.6.0.1032,,20170610,false,6.8.0.5.1596,,20170607,false,10.14.23624,,20170610,false,10.14.23624,,20170610,false,1.0.0.806,,20170610,true,1.0.0.2,Win32.Trojan.WisdomEyes.16070401.9500.9976,20170608,false,4.7.1.166,,20170610,true,1.3.1.0,PUA.MyPCBackup,20170610,true,15562,MSIL/MyPCBackup.D potentially unwanted,20170610,false,9.900.0.1004,,20170610,false,8.0.1489.320,,20170610,false,0.99.2.0,,20170610,false,15.0.1.13,,20170610,false,7.2,,20170610,true,1.0.76.17389,Riskware.Win32.Unwanted.dmgktv,20170610,false,1.0,,20170610,false,2014.3.20.0,,20170610,false,1.0.0.1,,20170610,false,3.0.3.1010,,20170610,false,4.0.1.883,,20170610,false,27254,,20170610,false,11.0.19100.45,,20170610,true,7.0.28.2020,Program.Unwanted.567,20170610,false,58730,,20170610,false,6.3.0.25415,,20170607,false,v2015,,20170610,false,4.98.0,,20170610,false,0.1.5.2,,20170610,false,5.4.30.7,,20170610,false,16.0.100,,20170610,false,1.0.0.207,,20170610,true,8.3.3.4,PUA/MyPCBackup.Gen,20170610,false,2013.8.14.323,,20170610,false,0.5.0,,20170515,false,1.1.13804.0,,20170610,false,4.2,,20170610,false,1.0,,20170610,true,A:25.12800B:25.9740,NSIS.Adware.MyPCBackup.E,20170610,false,3.9.0.17697,,20170610,false,1.0.1.9,,20170610,false,1.5.0.42,,20170610,false,3.12.26.4,,20170609,false,1.0,,20170610,true,28.0.0.1,Malware.Undefined!8.C (cloud:I1YBt1VpobT) ,20170610,true,5.5.1.3,Riskware.Agent!,20170608,false,1.0.0.12,,20170516,false,5.4.233.0,,20170610,false,8.0.1489.320,,20170610,false,4.6.4.2,,20170610,false,1.0,,20170420,false,1.0.0.1120,,20170610,00d468fa26813736cd14ff91e84f5e31fe30eaef6b35af44cafe540870ea7873-1497129945,7b890323abfe8f3bd33be0bc439076b5525d03b0,00D468FA26813736CD14FF91E84F5E31FE30EAEF6B35AF44CAFE540870EA7873,1,2017-06-10 21:25:45,https://www.virustotal.com/file/00d468fa26813736cd14ff91e84f5e31fe30eaef6b35af44cafe540870ea7873/analysis/1497129945/,"Scan finished, information embedded",60,11,00d468fa26813736cd14ff91e84f5e31fe30eaef6b35af44cafe540870ea7873,45922155c9628e11441aa869c6287bb7,false,9.740.0.1012,,20170610
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0E28BEDFBA37CEE5BD639AC86AC08A422C8944C3749CD2C5D7F5A0C2B37115B3,0,,,"The requested resource is not among the finished, queued or pending scans",,,,,,,,

有关CSV的显示方式,请参阅此链接:https://konklone.io/json/?id=e972cb1eeb3701f38062b1ea7d9d60df

有一种简单的方法吗?

更新

我们尝试了以下方法在Python 3.x中组合JSON文件,但生成的JSON文件无效:

import glob

read_files = glob.glob("*.json")
with open("merged_file.json", "w") as outfile:
    outfile.write('[{}]'.format(
        ','.join([open(f, "r").read() for f in read_files])))

2 个答案:

答案 0 :(得分:1)

就你的组合JSON对象而言,python附带了很多用于转换为JSON的工具。没有理由在字符串级别操纵它。你想要做的是解析JSON,组合字典,然后转换回JSON(除非你想直接使用该对象来生成你的CSV)。

import glob
import json


read_files = glob.glob("*.json")
main_object = {}
for f in read_files:
    with open(f, 'r') as current_file:
        raw = current_file.read()
        current_object = json.loads(raw)
        main_object.update(current_object)
with open("merged_file.json", "w") as outfile:
    raw = json.dumps(main_object)
    outfile.write(raw)
    outfile.close()

答案 1 :(得分:0)

以下代码将根据您的需要处理和合并您的JSON文件,并生成适合您格式的CSV。实际上最棘手的部分是将嵌套的dicts转换为你使用的列名。

import json
import pandas as pd
import glob

def flatten_dict(d_obj):

    for k, v in d_obj.items():

        if isinstance(v, dict):

            new_dict = {'{}/{}'.format(k,k2):v2 for k2, v2 in v.items()}
            yield from flatten_dict(new_dict)

        else:
            yield k, v

dict_list = []

files = glob.glob('*.json')

for file in files:
    with open(file, 'r') as f:
        data = f.read()
        j_data = json.loads(data)
        new_dict = {k:v for k,v in flatten_dict(j_data)}
        dict_list.append(new_dict)

# Write new raw JSON file with combined data
with open('merged_file.json', 'w') as new_f:
    new_f.write(json.dumps(dict_list))

# Convert list of dicts into dataframe and send to csv
df = pd.DataFrame(dict_list)
df.to_csv('merged_file.csv', index=False)