-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtoTreeJson.py
More file actions
40 lines (31 loc) · 1021 Bytes
/
Copy pathtoTreeJson.py
File metadata and controls
40 lines (31 loc) · 1021 Bytes
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
import csv
import json
class Node(object):
def __init__(self, name, size=None):
self.name = name
self.children = []
self.size = size
def child(self, cname, size=None):
child_found = [c for c in self.children if c.name == cname]
if not child_found:
_child = Node(cname, size)
self.children.append(_child)
else:
_child = child_found[0]
return _child
def as_dict(self):
res = {'name': self.name}
if self.size is None:
res['children'] = [c.as_dict() for c in self.children]
else:
res['value'] = self.size
return res
root = Node('Flare')
with open('adjustedwithValue.csv', encoding="utf8") as f:
reader = csv.reader(f)
next(reader)
for row in reader:
grp1, grp2, grp3, grp4, size = row
root.child(grp1).child(grp2).child(grp3).child(grp4, size)
with open('result.json', 'w') as json_file:
json.dump(root.as_dict(), json_file)