2015-04-05 62 views
1

我試圖創建我從中所產生的.csv一個JSON文件(D3)如下:Python的CSV以JSON(D3)

uat,soe1.1,deploy-mash-app40-uat,3.8.2.8,org.cgl.kfs.mas.mashonline,mashonline-ui-static 
uat,soe1.1,deploy-mash-app22-uat-has,1.0.1.RC1,org.cgl.kfs.mas.mashonline,realtime_balances_mims_feeder 
stg,soe1.1,deploy-coin-app2-stg,1.1.2,org.mbl.coin.ui.visormobile,vm-web-ui 
stg,soe1.1,deploy-coin-app2-stg,1.2.14,org.mbl.coin.ui.factfind,factfind-web-ui 

試了幾種方法,其中包括幾乎所有的帖子在StackOverflow中。 D3的JSON,我想有是這樣的:

{ 
    "name": "flare", 
    "children": [ 
     { 
      "name": "uat", 
      "children": [ 
       { 
        "name": "soe1.1", 
        "children": [ 
         { 
          "name": "deploy-mash-app40-uat", 
          "children": [ 
           { 
            "name": "mashonline-ui-static", 
            "children": [ 
             { 
              "name": "com.cgl.bfs.mas.mashonline", 
              "size": 3938 
             }, 
             { 
              "name": "3.8.2.8", 
              "size": 3812 
             } 
            ] 
           } 
          ] 
         }, 
         { 
          "name": "deploy-mash-app22-uat-has", 
          "children": [ 
           { 
            "name": "realtime_balances_mims_feeder", 
            "children": [ 
             { 
              "name": "1.0.1.RC1", 
              "size": 3534 
             }, 
             { 
              "name": "com.cgl.bfs.mas.mashonline", 
              "size": 5731 
             } 
            ] 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     }, 
     { 
      "name": "stg", 
      "children": [ 
       { 
        "name": "soe1.1", 
        "children": [ 
         { 
          "name": "deploy-coin-app2-stg", 
          "children": [ 
           { 
            "name": "vm-web-ui", 
            "children": [ 
             { 
              "name": "1.1.2", 
              "size": 3812 
             }, 
             { 
              "name": "com.mbl.coin.ui.visormobile", 
              "size": 6714 
             } 
            ] 
           }, 
           { 
            "name": "factfind-web-ui", 
            "children": [ 
             { 
              "name": "1.2.14", 
              "size": 5731 
             }, 
             { 
              "name": "com.mbl.coin.ui.factfind", 
              "size": 7840 
             } 
            ] 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 

基本上,有一個最後兩倍中的值作爲列的兄弟姐妹提前4 謝謝(我是新手python)。

試圖 Link1 Link2和其他很多環節的,但是沒辦法,我可以讓它工作

我有它運行的代碼如下(由於上述鏈接之一),但我發現它難以在到達小區時添加「名稱」,「兒童」節點。

import json 
import csv 

tree = {} 
name = "name" 
children = "children" 
reader = csv.reader(open("cleaned_new_test.txt", 'rb')) 
reader.next() 
for row in reader: 
    print tree 
    subtree = tree 
    for i, cell in enumerate(row): 
     if cell: 
      if cell not in subtree: 
       subtree[cell] = {} if i<len(row)-1 else 1 
       print subtree 
      subtree = subtree[cell] 

print json.dumps(tree, indent=4) 
+0

D3還可以讀取的CSV文件是否有幫助[見這裏](https://github.com/mbostock/ d3/wiki/CSV) – 2015-04-05 02:01:00

+0

你從哪裏得到'size'? – jedwards 2015-04-05 02:15:08

+0

@jedwards大小是我隨機添加的一個值,只是爲了符合D3的格式。現在沒有任何意義。 – gameshark 2015-04-05 02:26:50

回答

2

這裏有一種方法從您的CSV文件才能到JSON:

import csv 
from collections import OrderedDict 
import json 

def fmt(d): 
    l = [] 
    for (k,v) in d.items(): 
     j = OrderedDict() 
     j['name'] = k 
     if isinstance(v, dict): 
      j['children'] = fmt(v) 
     elif isinstance(v, list): 
      for (k,v) in v: 
       j[k] = v 
     l.append(j) 
    return l 

# Build OrderedDict 
d1 = OrderedDict() 
with open('input.txt') as f: 
    reader = csv.reader(f,) 
    for row in reader: 
     print(row) 
     # Extract the columns you want to use as "leaves" 
     leaves = [row[-2], row[-3]] 
     for l in leaves: row.remove(l) 
     # Build a dictionary based on remaining row elements 
     ctx = d1 
     for e in row: 
      if e not in ctx: ctx[e] = OrderedDict() 
      ctx = ctx[e] 
     # Re-insert leaves 
     for l in leaves: 
      ctx[l] = None 

print(json.dumps(d1, indent=4)) 
print('---') 


# Insert missing items (ctx = context) 
ctx = d1['uat']['soe1.1']['deploy-mash-app40-uat']['mashonline-ui-static'] 
ctx['org.cgl.kfs.mas.mashonline'] = [('size', 3938)] 
ctx['3.8.2.8']      = [('size', 3812)] 

ctx = d1['uat']['soe1.1']['deploy-mash-app22-uat-has']['realtime_balances_mims_feeder'] 
ctx['1.0.1.RC1']     = [('size', 3534)] 
ctx['org.cgl.kfs.mas.mashonline'] = [('size', 5731)] 

ctx = d1['stg']['soe1.1']['deploy-coin-app2-stg']['vm-web-ui'] 
ctx['1.1.2']      = [('size', 3812)] 
ctx['org.mbl.coin.ui.visormobile'] = [('size', 6714)] 

ctx = d1['stg']['soe1.1']['deploy-coin-app2-stg']['factfind-web-ui'] 
ctx['1.2.14']      = [('size', 5731)] 
ctx['org.mbl.coin.ui.factfind']  = [('size', 7840)] 

# Wrap "formatted" in another dictionary 
d2 = {"name": "flare", "children": fmt(d1)} 

j = json.dumps(d2, indent=4) 
print(j) 

輸出:

 
{ 
    "name": "flare", 
    "children": [ 
     { 
      "name": "uat", 
      "children": [ 
       { 
        "name": "soe1.1", 
        "children": [ 
         { 
          "name": "deploy-mash-app40-uat", 
          "children": [ 
           { 
            "name": "mashonline-ui-static", 
            "children": [ 
             { 
              "name": "org.cgl.kfs.mas.mashonline", 
              "size": 3938 
             }, 
             { 
              "name": "3.8.2.8", 
              "size": 3812 
             } 
            ] 
           } 
          ] 
         }, 
         { 
          "name": "deploy-mash-app22-uat-has", 
          "children": [ 
           { 
            "name": "realtime_balances_mims_feeder", 
            "children": [ 
             { 
              "name": "org.cgl.kfs.mas.mashonline", 
              "size": 5731 
             }, 
             { 
              "name": "1.0.1.RC1", 
              "size": 3534 
             } 
            ] 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     }, 
     { 
      "name": "stg", 
      "children": [ 
       { 
        "name": "soe1.1", 
        "children": [ 
         { 
          "name": "deploy-coin-app2-stg", 
          "children": [ 
           { 
            "name": "vm-web-ui", 
            "children": [ 
             { 
              "name": "org.mbl.coin.ui.visormobile", 
              "size": 6714 
             }, 
             { 
              "name": "1.1.2", 
              "size": 3812 
             } 
            ] 
           }, 
           { 
            "name": "factfind-web-ui", 
            "children": [ 
             { 
              "name": "org.mbl.coin.ui.factfind", 
              "size": 7840 
             }, 
             { 
              "name": "1.2.14", 
              "size": 5731 
             } 
            ] 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 

這不是最漂亮的,但它能夠完成任務。

一些注意事項:

  • 添加size元素之後的事實是醜陋的,有可能是一個更好的方式來做到這一點。 (我指的是以「Insert missing items」爲註釋開頭的代碼)。在本節中,您可以指定其他鍵:值對以添加爲列表(鍵,值)2元組。
  • 這部分可能已被寫爲:

    # Insert missing items (ctx = context) 
    d1['uat']['soe1.1']['deploy-mash-app40-uat']['mashonline-ui-static']['org.cgl.kfs.mas.mashonline']    = [('size', 3938)] 
    d1['uat']['soe1.1']['deploy-mash-app40-uat']['mashonline-ui-static']['3.8.2.8']         = [('size', 3812)] 
    d1['uat']['soe1.1']['deploy-mash-app22-uat-has']['realtime_balances_mims_feeder']['1.0.1.RC1']     = [('size', 3534)] 
    d1['uat']['soe1.1']['deploy-mash-app22-uat-has']['realtime_balances_mims_feeder']['org.cgl.kfs.mas.mashonline'] = [('size', 5731)] 
    d1['stg']['soe1.1']['deploy-coin-app2-stg']['vm-web-ui']['1.1.2']            = [('size', 3812)] 
    d1['stg']['soe1.1']['deploy-coin-app2-stg']['vm-web-ui']['org.mbl.coin.ui.visormobile']       = [('size', 6714)] 
    d1['stg']['soe1.1']['deploy-coin-app2-stg']['factfind-web-ui']['1.2.14']          = [('size', 5731)] 
    d1['stg']['soe1.1']['deploy-coin-app2-stg']['factfind-web-ui']['org.mbl.coin.ui.factfind']      = [('size', 7840)] 
    

    (不ctx參考的東西)。我只是使用已定義的ctx作爲字典結構中的一個位置,然後使用它來設置更深的字典值,以使行更短且更易於管理。

  • 預期的json好多了,但還是有點關閉。也就是說,您指定標識符如com.cgl.bfs.mas.mashonline,但您的csv具有org.cgl.bfs.mas.mashonline(「com」與「org」)。另外,json中的「葉子」元素的順序不一致。在我的腳本輸出的json中,第5列出現在第4列之前。在你的json中,第一個元素以這種方式出現,但最後三個出現時交換順序(在5之前4)。如果你想要這個交換順序,變更:

    leaves = [row[-2], row[-3]] 
    

    leaves = [row[-3], row[-2]]