1 回答
TA贡献1786条经验 获得超12个赞
你正在构建你的 dict 错误。
尝试这样的事情
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 = dict(
name=source,
group=1
)
nodes.append(node_arr)
nodes_dict[source] = counter
counter += 1
if target not in nodes_dict:
node_arr = dict(
name=target,
group=1
)
nodes.append(node_arr)
nodes_dict[target] = counter
counter += 1
json.dumps(nodes,outputfile)
添加回答
举报