2017-05-03 174 views
3

我读这些问题:将csv转换为JSON树结构?

但是我仍然无法csv文件转换为JSON的层次结构。我在stackoverflow上找到的脚本是特定于某个问题的。比方说,有三个变量,必须进行分组:

condition target sub 
oxygen  tree G1 
oxygen  tree G2 
water  car  G3 
water  tree GZ 
fire  car  GTD 
oxygen  bomb GYYS 

这将导致在这样的JSON文件(据我试过):

oxygen 
    - tree 
     - G1 
     - G2 
    - bomb 
     -GYYS 
water 
    - car 
     - G3 
    - tree 
     -GZ 
fire 
    - car 
     - GTD 

而这些都必须分组在嵌套结构,如:

{ 
    "name": "oxygen", 
    "children": [ 
    { 
    "name": "tree", 
    "children": [ 
     {"name": "G1"}, 
     {"name": "G2"}, 
     {"name": "GYYS"} 
    ] 
    }, 
    { 
    "name": "bomb", 
     "children": [ 
     {"name": "GYYS"} 
    ] 
    } 
    ] 
} 
etc. 

我试着在这个网站但是我不能做一个通用的功能,可以作出这样一个flare.json每个脚本。我可以发布我的代码,但是这和上面提供的链接一样。所以我要求一个简单的代码(或者一个可以帮助我的例子)将它转换成类似结构的flare.JSON。

+0

你的“三个变量”不出现在你的json例子的任何地方,没有明显的联系。因此,请详细解释如何从csv文件中的数据生成json结构 –

+0

我并不熟悉JSON,但我试图给出一个我想要的简短示例(请参阅编辑)@ChristianKönig – CodeNoob

+0

您可以,请将您的json示例复制并粘贴到http://jsoneditoronline.org/这个viewer/validator中,并且至少为我们提供有效的json exmaple,因为您的json示例不是有效的json。还请提供规则(高级逻辑)如何将csv转换为json,什么是儿童,为什么? 这些信息将有助于给你一个有效的答案。 – Hett

回答

4

使用collections标准库中的defaultdict使分层结构中的很多问题变得简单和可解。所以我为你的问题开发了一个样本解决方案。但是,在运行脚本之前,请确保您有逗号分隔的csv文件(名为test.csv),或者您可以在那里更改逻辑csv reader

这是我测试过脚本的csv文件。

condition, target, sub, dub 
oxygen,tree,G1,T1 
oxygen,tree,G2,T1 
oxygen,tree,G2,T2 
water,car,G3,T1 
water,tree,GZ,T1 
water,tree,GZ,T2 
fire,car,GTD,T3 
oxygen,bomb,GYYS,T1 

从技术上讲,脚本应该适用于各种尺寸的csv文件。但是你需要自己测试才能确定。

import csv 
from collections import defaultdict 


def ctree(): 
    """ One of the python gems. Making possible to have dynamic tree structure. 

    """ 
    return defaultdict(ctree) 


def build_leaf(name, leaf): 
    """ Recursive function to build desired custom tree structure 

    """ 
    res = {"name": name} 

    # add children node if the leaf actually has any children 
    if len(leaf.keys()) > 0: 
     res["children"] = [build_leaf(k, v) for k, v in leaf.items()] 

    return res 


def main(): 
    """ The main thread composed from two parts. 

    First it's parsing the csv file and builds a tree hierarchy from it. 
    Second it's recursively iterating over the tree and building custom 
    json-like structure (via dict). 

    And the last part is just printing the result. 

    """ 
    tree = ctree() 
    # NOTE: you need to have test.csv file as neighbor to this file 
    with open('test.csv') as csvfile: 
     reader = csv.reader(csvfile) 
     for rid, row in enumerate(reader): 

      # skipping first header row. remove this logic if your csv is 
      # headerless 
      if rid == 0: 
       continue 

      # usage of python magic to construct dynamic tree structure and 
      # basically grouping csv values under their parents 
      leaf = tree[row[0]] 
      for cid in range(1, len(row)): 
       leaf = leaf[row[cid]] 

    # building a custom tree structure 
    res = [] 
    for name, leaf in tree.items(): 
     res.append(build_leaf(name, leaf)) 

    # printing results into the terminal 
    import json 
    print(json.dumps(res)) 


# so let's roll 
main() 

下面是从结果的JSON段:

{ 
    "name": "oxygen", 
    "children": [ 
     { 
     "name": "tree", 
     "children": [ 
      { 
      "name": "G2", 
      "children": [ 
       { 
       "name": "T2" 
       }, 
       { 
       "name": "T1" 
       } 
      ] 
      }, 
      { 
      "name": "G1", 
      "children": [ 
       { 
       "name": "T1" 
       } 
      ] 
      } 
     ] 
     }, 
     { 
     "name": "bomb", 
     "children": [ 
      { 
      "name": "GYYS", 
      "children": [ 
       { 
       "name": "T1" 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    } 

请让我知道,如果您有任何疑问和问题。 快乐pythonning;)

+0

谢谢你这么多!,更容易理解,因为所有这些堆栈上的其他答案 – CodeNoob

+0

很高兴这是有帮助的! – Hett