0

Python(またはプッシュ、Javascript)でこれを行うエレガントな方法があると確信していますが、私の人生ではそれを見ることができません...

次の形式の CSV ファイルがあります。

ID, Name, Description
A, A-name,
A100, A100-name, A100-desc
A110, A110-name, A110-desc
A111, A111-name, A111-desc
A112, A112-name, A112-desc
A113, A113-name, A113-desc
A120, A120-name, A120-desc
A131, A131-name, A131-desc
A200, A200-name, A200-desc
B, B-name,
B100, B100-name, B100-desc
B130, B130-name, B130-desc
B131, B131-name, B131-desc
B140, B140-name, B140-desc

theJITでデータを視覚化できるように、階層的な JSON 構造を生成したいと考えています。

var json = {  
  "id": "aUniqueIdentifier",  
  "name": "usually a nodes name",  
  "data": {  
    "some key": "some value",  
    "some other key": "some other value"  
   },  
  "children": [ *other nodes or empty* ]  
}; 

私の計画は、ID を id に、名前を名前に、説明を data.desc にマップし、次のように階層を編成することでした。

  • ルートは A と B の親です
  • A は A100 と A200 の親です
  • A100 は A110 と A120 の親です
  • A110 は A111、A112、および A113 の親です
  • B は B100 の親です
  • B100 は B130 と B140 の親です
  • B130 は B131 の親です

A100 が A131 の親である (予期された A130 が存在しない)、通常の ID による順序付けでは病的なケースもあります。

これに対するエレガントなPythonの解決策を見つけたいと思っていましたが、病理学的なケースを無視しても、現時点では私を打ち負かしています...

4

1 に答える 1

2

これはそれを行います...

import csv
import json

class Node(dict):
    def __init__(self, (nid, name, ndescr)):
        dict.__init__(self)
        self['id'] = nid
        self['name'] = name.lstrip() # you have badly formed csv....
        self['description'] = ndescr.lstrip()
        self['children'] = []

    def add_node(self, node):
        for child in self['children']:
            if child.is_parent(node):
                child.add_node(node)
                break
        else:
            self['children'].append(node)

    def is_parent(self, node):
        if len(self['id']) == 4 and self['id'][-1] == '0':
            return node['id'].startswith(self['id'][:-1])
        return node['id'].startswith(self['id'])

class RootNode(Node):
    def __init__(self):
        Node.__init__(self, ('Root', '', ''))

    def is_parent(self, node):
        return True

def pretty_print(node, i=0):
    print '%sID=%s NAME=%s %s' % ('\t' * i, node['id'], node['name'], node['description'])
    for child in node['children']:
        pretty_print(child, i + 1)

def main():
    with open('input.csv') as f:
        f.readline() # Skip first line
        root = RootNode()
        for node in map(Node, csv.reader(f)):
            root.add_node(node)

    pretty_print(root)
    print json.dumps(root)

if __name__ == '__main__':
    main()
于 2011-09-13T22:15:57.893 に答える