-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprint_csv.py
More file actions
executable file
·45 lines (35 loc) · 1.06 KB
/
print_csv.py
File metadata and controls
executable file
·45 lines (35 loc) · 1.06 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
#!/usr/bin/env python2.7
from __future__ import print_function
import json
import os
import sys
class DictOfLists(dict):
def __init__(self):
self.__names = []
super(DictOfLists, self).__init__()
def add(self, d, name):
self.__names.append(name)
for k,v in d.iteritems():
if not self.has_key(k):
self[k] = []
self[k].append(v)
def printCSV(self):
print(','.join([''] + self.__names))
for k in self.iterkeys():
print(','.join([k] + [ str(e) for e in self[k]]))
def flatDict(d, sep, prefix=None):
ret = {}
for k,v in d.iteritems():
sk = str(k)
if prefix:
sk = prefix + str(sep) + sk
if type(v) is dict:
ret.update(flatDict(v, sep, sk))
else:
ret[sk] = v
return ret
dol = DictOfLists()
for json_file in filter(lambda s: s.rfind('json')>=0, sys.argv):
dol.add(flatDict(json.loads(open(json_file).read()),'.'),
os.path.basename(os.path.split(json_file)[0]))
dol.printCSV()