title: Multiple Csv to json with no repetitive childrens
body: <p>I have a csv columns as follows, now I am trying to convert it to name/Children/Size format as required in D3 in JSON. There are repetetive Childrens Occuring For example 
  In name ="type" there is children ="young", size = 400000</p>
  
  <pre><code>L1       L2     L3         L4        L5      L6          Size
  Type    cars    young      young     young   young      40000
  Type    cars    student    US        US      US         10000
  Type    cars    student    UK        UK      UK         20000
  Type    cars    Graduates  Young    India    Delhi      20000
  Type    cars    Graduates  Old      UK       London     30000
  Type    Bike    Undergrads CB       CB       UNC        6000
  prime   prime   prime      prime    prime   prime       600
  </code></pre>
  
  <p>My output I am getting is :</p>
  
  <pre><code>{
      "name": "Segments",
      "children": [
          {
              "name": "Type",
              "children": [
                  {
                      "name": "cars",
                      "children": [
                          {
                              "name": "young",
                              "children": [
                                  {
                                      "name": "young",
                                      "children": [
                                          {
                                              "name": "young",
                                              "children": [
                                                  {
                                                      "name": "young",
                                                      "size": "40000"
                                                  }
                                              ]
                                          }
                                      ]
                                  }
                              ]
                          },
                          {
                              "name": "student",
                              "children": [
                                  {
                                      "name": "US",
                                      "children": [
                                          {
                                              "name": "US",
                                              "children": [
                                                  {
                                                      "name": "US",
                                                      "size": "10000"
                                                  }
                                              ]
                                          }
                                      ]
                                  },
                                  {
                                      "name": "UK",
                                      "children": [
                                          {
                                              "name": "UK",
                                              "children": [
                                                  {
                                                      "name": "UK",
                                                      "size": "20000"
                                                  }
                                              ]
                                          }
                                      ]
                                  }
                              ]
                          }
                      ]
                  }
              ]
          },
          {
              "name": "prime",
              "children": [
                  {
                      "name": "prime",
                      "children": [
                          {
                              "name": "prime",
                              "children": [
                                  {
                                      "name": "prime",
                                      "children": [
                                          {
                                              "name": "prime",
                                              "children": [
                                                  {
                                                      "name": "prime",
                                                      "size": "600"
                                                  }
                                              ]
                                          }
                                      ]
                                  }
                              ]
                          }
                      ]
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>expecting output is:</p>
  
  <pre><code>{
      "name": "Segments",
      "children": [
          {
              "name": "Type",
              "children": [
                  {
                      "name": "cars",
                      "children": [
                          {
                          "name": "young",
                          "size": "40000"
                          }
                                  ]
  
  
                          },
                          {
                              "name": "student",
                              "children": [
                                  {
                                   "name": "US",
                                   "size": "10000"
                                  }
  
                                  {
                                  "name": "UK",
                                  "size": "20000"
                                  }
                                              ]
                                          }
                                      ]
                                  },
  
          {
          "name": "prime",
          "size": "600"
          }
          ]
          }
  </code></pre>
  
  <p>I am using Following code:</p>
  
  <pre><code>import json
  import csv
  
  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['size'] = self.size
          return res
  
  
  root = Node('Segments')
  
  with open('C:\\Users\\G01172472\\Desktop\\Book3.csv', 'r') as f:
      reader = csv.reader(f)
      p = list(reader)
      for row in range(1, len(p)):
          grp1, grp2, grp3, grp4, grp5, grp6, size = p[row]
          root.child(grp1).child(grp2).child(grp3).child(grp4).child(grp5).child(grp6, size)
  
  print(json.dumps(root.as_dict(), indent=4))
  </code></pre>