我正在尝试使用下面的代码为我的图表生成JSON文件。
8 else:
9 G[node][type] = '2'
---> 10 d = json_graph.node_link_data(G)
11 json.dump(d, open('data.json','w'))
抛出错误属性错误:'str'对象没有属性'items'
如果我从图表中删除属性('图层'),我可以保存为JSON格式。我的目标是使用属性保存图形。 这是使用networkx创建JSON的正确方法吗?
AttributeError Traceback(最近一次调用最后一次) in()
90 dict(chain(d.items(),
91 [(source, mapping[u]), (target, mapping[v])]))
---> 92 for u, v, d in G.edges_iter(data=True)]
93
94 return data
C:\ Users \ ljose \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ networkx \ readwrite \ json_graph \ node_link.py in node_link_data(G,attrs)
90 dict(chain(d.items(),
91 [(source, mapping[u]), (target, mapping[v])]))
---> 92 for u, v, d in G.edges_iter(data=True)]
93
94 return data
C:\ Users \ ljose \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ networkx \ readwrite \ json_graph \ node_link.py in(.0)
$scope.addVideo = function (data) {
console.log('scope tutorial', tutorial);
$http.post('/tutorials/' + tutorial.id + '/videos/', $scope.formData)
.then((response) => {
$window.tutorial.videos.push(response.data.video);
window.location = '/tutorials' + tutorial.id;
})
}
AttributeError:'str'对象没有属性'items'
答案 0 :(得分:1)
错误来自于G[node][type] = 1
实际上将权重1
分配给边(node, type)
而不是将属性type
分配给节点node
值{ {1}}。以下应该解决它:
1