【发布时间】:2020-07-27 22:06:47
【问题描述】:
我正在使用这个 Python 函数编写一个 networkx 图:
from networkx.readwrite import json_graph
def save_json(filename,graph):
g = graph
g_json = json_graph.node_link_data(g)
json.dump(g_json,open(filename,'w'),indent=2)
并尝试使用以下方式加载图表:
def read_json_file(filename):
graph = json_graph.loads(open(filename))
return graph
读取函数取自here。
我的问题是这给了我错误:
AttributeError: 'module' object has no attribute 'load'
这是有道理的,因为 networkx 文档中没有 load method。
那么,我的问题是如何加载包含 networkx 图的 json 文件?
【问题讨论】:
-
json_graph.loads或json_graph.load? -
也需要
import json声明...