-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsondatahandle.py
84 lines (70 loc) · 2.12 KB
/
jsondatahandle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import numpy
import json
import csv
import fileinput
Metrix=[]
Data=[]
class JsonHandle:
def __init__(self,filepath,filename):
self.filepath=filepath
self.filename=filename
def get_Metrix(self,k,v):
if isinstance(v,dict):
for kk in v.keys():
if kk!="eth1":
tmp=k+"_"+kk
self.get_Metrix(tmp,v[kk])
elif isinstance(v,list):
if len(v)!=0:
for i in range(len(v)):
tmp = k+"_"+str(i)
self.get_Metrix(tmp,v[i])
else:
if(k not in Metrix):
Metrix.append(k)
def get_value(self,k,v):
if isinstance(v,dict):
for kk in v.keys():
if kk!="eth1":
tmp=k+"_"+kk
self.get_value(tmp,v[kk])
elif isinstance(v,list):
if len(v)!=0:
for i in range(len(v)):
tmp = k+"_"+str(i)
self.get_value(tmp,v[i])
else:
i=Metrix.index(k)
Data[i]=v
def handle(self):
flag=True
length=0
for line in fileinput.input(self.filepath):
jsondata=json.loads(line)
if len(Metrix)==0:
for key in jsondata.keys():
self.get_Metrix(key, jsondata[key])
length=len(Metrix)
global Data
Data=[0]*length
for key in jsondata.keys():
self.get_value(key,jsondata[key])
with open(self.filename, "a") as csvfile:
writer = csv.writer(csvfile)
if flag:
writer.writerow(Metrix)
flag=False
writer.writerow(Data)
jh=JsonHandle("dataset1/sql.json","dataset1/sql.csv")
jh.handle()
# jh1=JsonHandle("./data/mariadb3.json","mariadb3.csv")
# jh1.handle()
#
# jh2=JsonHandle("./data/mariadb1.json","mariadb1.csv")
# jh2.handle()
#
# jh1=JsonHandle("./data/memcached.json","memcached.csv")
# jh1.handle()
#
# jh2=JsonHandle("./data/nginx.json","nginx.csv")
# jh2.handle()