我们如何通过使用csv数据或作为列表提供的数据在python中动态创建json文件
data.csv 看起来像这样
busi,biz
thank,pump
thank,such
thank,merri
它显示了图中节点之间的链接/边。
我正在尝试构建节点和数组的 json,但出现 json 序列化错误。
nodes = []
nodes_dict = {}
counter = 0
edges = open(edges_file)
for line in edges:
line = line.replace("\n","")
source = line.split(",")[0]
target = line.split(",")[1]
if source not in nodes_dict:
node_arr = {"name:" + source + "," + "group:1"}
nodes.append(node_arr)
nodes_dict[source] = counter
counter += 1
if target not in nodes_dict:
node_arr = {"name:"+ target + "," + "group:1"}
nodes.append(node_arr)
nodes_dict[target] = counter
counter += 1
json.dumps(nodes,outputfile)
但我收到此错误
类型错误:{'name:busi,group:1'} 不是 JSON 可序列化的
我想要的输出
"nodes":[
{"name":"Myriel","group":1},
{"name":"Napoleon","group":1},
{"name":"Mme.Hucheloup","group":8}],
"links":
[{"source":1,"target":0,"value":1},{"source":2,"target":0,"value":8},
{"source":3,"target":0,"value":10},{"source":3,"target":2,"value":6}}
}
开满天机
相关分类