Python to parent/child JSON -


i'm trying use python turn data csv json format found here: https://gist.github.com/mbostock/1093025 can modify http://d3js.org/ examples.

i have found posts on how similar transformations, nothing nested {'name': name, 'children' = []} format.

for test.csv:

team,task,country,id team a,processing,ca,5 team a,review,ca,45 team b,processing,us,76 team b,processing,ca,676 team b,support,us,2345 team c,processing,us,67 team c,review,us,734 team c,support,us,34 

output should like:

{  "name": "flare",  "children": [   {    "name": "team a",    "children": [     {      "name": "processing",      "children": [       {"name": "ca", "size": 5}      ]     },     {      "name": "review",      "children": [       {"name": "ca", "size": 45}      ]     }    ]   },   {    "name": "team b",    "children": [     {      "name": "processing",      "children": [       {"name": "us", "size": 76},       {"name": "ca", "size": 676}      ]     },     {      "name": "support",      "children": [       {"name": "us", "size": 2345}      ]     }    ]   },    {    "name": "team c",    "children": [     {      "name": "processing",      "children": [       {"name": "us", "size": 67}      ]     },     {      "name": "review",      "children": [       {"name": "us", "size": 734}      ]     },     {      "name": "support",      "children": [       {"name": "us", "size": 34}      ]     }    ]   }  ] } 

this far have been able (i know it's pretty bad):

import csv import json  children = []  #create list of lists csv reader = csv.reader(open('//corp.bloomberg.com/pn-dfs/alllinks/users/jdesilvio/desktop/test.csv', 'rb')) reader.next()  row in reader:     children.append(row)  #create tree root tree = {'name': "flare", 'children': []}  #create generic subtree subtree = {'name': 0, 'children': []}  in children:     #if first element in doesn't equal name, know it's different group      if i[0] != subtree['name']:         #so append current group         tree['children'].append({'name': subtree['name'], 'children': subtree['children']})         #start new group         subtree['children'] = []         #and rename subtree         subtree['name'] = i[0]     else:         #then start appending pieces new group         subtree['children'].append(i[1:len(i)])  #remove generic starting name tree['children'] = tree['children'][1:]  print json.dumps(tree, indent=1) 

which yields:

{  "name": "flare",   "children": [   {    "name": "team a",     "children": [     [      "review",       "ca",       "45"     ]    ]   },    {    "name": "team b",     "children": [     [      "processing",       "ca",       "676"     ],      [      "support",       "us",       "2345"     ]    ]   }  ] } 

this looks headed in right direction, if able first level nested, i'm not sure how nest more levels in generic way.

populate tree clear solution. however, using dict traversing not idea. suggest create helper class each tree node, use populating data , convert result json:

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 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 none:             res['children'] = [c.as_dict() c in self.children]         else:             res['size'] = self.size         return res   root = node('flare')  open('/tmp/test.csv', 'r') f:     reader = csv.reader(f)     reader.next()     row in reader:         grp1, grp2, grp3, size = row         root.child(grp1).child(grp2).child(grp3, size)  print json.dumps(root.as_dict(), indent=4) 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -