我試圖創建我從中所產生的.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)
D3還可以讀取的CSV文件是否有幫助[見這裏](https://github.com/mbostock/ d3/wiki/CSV) – 2015-04-05 02:01:00
你從哪裏得到'size'? – jedwards 2015-04-05 02:15:08
@jedwards大小是我隨機添加的一個值,只是爲了符合D3的格式。現在沒有任何意義。 – gameshark 2015-04-05 02:26:50