-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconvert_json.py
More file actions
executable file
·62 lines (55 loc) · 1.45 KB
/
convert_json.py
File metadata and controls
executable file
·62 lines (55 loc) · 1.45 KB
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
#!/usr/bin/python
import json
import sys
if len(sys.argv)!=2:
print >>sys.sdterr,sys.argv[0]," file.json"
sys.exit(1)
with open(sys.argv[1]) as f:
datas=json.loads(f.read())
if type(datas)==dict:
# from motor based to time based
times={}
for m in datas.keys():
for t,v in datas[m]:
if times.has_key(t)==False:
times[t]={}
times[t][m]=v
k=times.keys()
k.sort()
print "["
for x in k:
print " {"
print ' "time":',x,
for m in times[x]:
print ',\n "%s"'%(m.strip()),':',times[x][m],
print '\n },'
print "]"
else:
# from time based to motor based
motors={}
for v in datas:
for m in v.keys():
if m!='time':
if motors.has_key(m)==False:
motors[m]=[]
motors[m].append((v['time'],v[m]))
mnames=motors.keys()
mnames.sort(key= lambda x: x[2:]+x[0:2])
print '{'
for i in range(len(mnames)):
m=mnames[i]
print ' "%s": ['%(m)
motors[m].sort()
first=True
for t,v in motors[m]:
if first:
first=False
else:
print ',\n',
print ' [',t,',',v,']',
print '\n'
if i==len(mnames)-1:
print ' ]'
else:
print ' ],'
print '}'