忽略键[Behavior]不存在的特定json文件

2024-09-24 22:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理一个包含多个.JSON文件的布谷鸟沙盒数据集的庞大数据集,我必须在JSON文件的行为部分创建一个包含API统计信息的CSV文件,但如果JSON文件没有特定文件,代码将停止执行

这是我的节目

   import pandas as pd
# As of Pandas 1.01, json_normalize as pandas.io.json.json_normalize is deprecated and is now exposed in the top-level namespace.
from pandas.io.json import json_normalize
from pathlib import Path
import json
import os

bkey=[]
infoList=[]
signaturesList=[]
fileOpsList=[]
irmaList=[]
suricataList=[]
virustotalList=[]
sysmonList=[]
resubmitList=[]
snortList=[]
behaviorList=[]
memoryList=[]
debugList=[]
#mispList=[]
targetList=[]
networkList=[]
metadataList=[]
list2=[]

#print(pathList)
path_to_json = 'C:/Users/skdk/Desktop/Ransomware-API/Benign/'

for file_name in [file for file in os.listdir(path_to_json) if file.endswith('.json')]:
    with open(path_to_json + file_name, encoding ='utf-8') as json_file:
        data = json.load(json_file) 
        #print(data)
        behaviorList.append(str(data['behavior']))
    
# for path in path_to_json:
#     p = Path(path)
#     #print(p)
#     # read json50
#     with p.open('r', encoding='utf-8') as f:
#         data = json.loads(f.read())
#         #print(p)
  #  behaviorList.append(str(data['behavior'])) 


apiStatsList = []
for behavior in behaviorList:
    for key,value in eval(behavior)['apistats'].items():
        fileName = str(pathList[behaviorList.index(behavior)][:pathList[behaviorList.index(behavior)].index('.json')])+"/" + str(key)
        list2.append(fileName)
        apiStatsList.append(value)
    print(fileName)

dataset2= {}
for key,value in apiStatsList[0].items():
    dataset2[key] = [value]

count = 1
for apiStat in apiStatsList[1:]:
    for key,value in apiStat.items():
        if(key in dataset2):
            while(len(dataset2[key])!=count):
                dataset2[key].append(0)
            dataset2[key].append(apiStat[key])
        else:
            tempList=[0]*(count)
            tempList.append(value)
            dataset2[key] = tempList
    count=count+1

                
dataset2['Directory']=list2
df2 = pd.DataFrame.from_dict(dataset2, orient='index')
df2 = df2.transpose()
df2 = df2.fillna(0)
df2=df2.set_index('Directory')
#df2
df2.to_csv('Benign.csv')

我得到一个如下错误

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-16-fc19a9a3c2d1> in <module>
     34         data = json.load(json_file)
     35         #print(data)
---> 36         behaviorList.append(str(data['behavior']))
     37 
     38 # for path in path_to_json:

KeyError: 'behavior'

感谢您的帮助


Tags: topathkeyinjsonfordatavalue
1条回答
网友
1楼 · 发布于 2024-09-24 22:28:35

放进去

        try:
             'your code'
        except KeyError:
            'your code in case the json doesn't have behaviour. It could skip to the next file for example.'
```

It would catch any or specified error. And since you've said you are interested only in files with behaviour key, I think it should help you. 

相关问题 更多 >