Python csv到json(D3)
我试图从一个.csv文件创建一个JSON文件(用于D3),我生成了如下文件:Python csv到json(D3),python,json,csv,d3.js,Python,Json,Csv,D3.js,我试图从一个.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-
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中的几乎所有帖子。
我想要的D3JSON是这样的:
{
"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的新手)
尝试
还有很多其他的链接,但我没办法让它工作
我让它运行的代码如下(感谢上面的一个链接),但我发现在到达单元格时很难添加“name”、“children”节点
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)
导入json
导入csv
树={}
name=“name”
children=“children”
reader=csv.reader(打开(“cleaned_new_test.txt”,“rb”))
reader.next()
对于读取器中的行:
打印树
子树=树
对于i,枚举(行)中的单元格:
如果单元:
如果单元格不在子树中:
子树[cell]={}如果我这里有一种从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
}
]
}
]
}
]
}
]
}
]
}
(没有ctx
参考资料)。我只是使用definedctx
作为字典结构中的一个位置,然后使用它来设置更深的字典值,使行更短,更易于管理
预期的json要好得多,但仍然有点差。也就是说,您可以指定标识符,如com.cgl.bfs.mash.mashonline
,但您的csv具有org.cgl.bfs.mashonline
(“com”与“org”)。此外,json中“叶”元素的顺序不一致。在json my脚本输出中,第5列出现在第4列之前。在json中,第一个元素以这种方式显示,但最后三个元素以交换顺序显示(4比5)。如果需要此交换订单,请更改:
leaves = [row[-2], row[-3]]
到
D3还可以读取csv文件,如果这有助于您从何处获取size
信息?@jedwards大小只是我随机添加的一个值,只是为了符合D3的格式。现在没有任何意义了。@RúnarBerg谢谢,但D3似乎在将csv格式化为我们自己的自定义格式json方面没有提供帮助。您能否验证您编写的json是您想要的json?具体来说,输出的第二个“一半”的结构与第一个不同。而且,org.mbl.coin.ui.factfind
看起来像是重复的。在前半部分中,第4列和第5列是第6列的后代。在第三个条目中,第4列和第6列是第5列的后代。在最后一个条目中,第5列和第6列是第5列的后代。
leaves = [row[-2], row[-3]]
leaves = [row[-3], row[-2]]