读取json图networkx文件 [英] read json graph networkx file

查看:569
本文介绍了读取json图networkx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下简单的python函数编写networkx图:

I'm writing a networkx graph by using this simple python function:

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)

并尝试使用以下方式加载图形:

and was trying to load the graph using:

def read_json_file(filename):
    graph = json_graph.loads(open(filename))
    return graph

读取功能来自此处:

http://nullege.com/codes/search/networkx.readwrite. json_graph.load

我的问题是,这给了我错误:

My problem is that is gives me the error:

AttributeError:模块"对象没有属性加载"

AttributeError: 'module' object has no attribute 'load'

这很有意义,因为显然从Networkx文档中没有加载方法:

which makes sense, since apparently from Networkx documentation there is no load method:

https://networkx. github.io/documentation/latest/reference/readwrite.json_graph.html#module-networkx.readwrite.json_graph

所以,我的问题是如何加载包含networkx图的json文件?

So, my question is how to I load a json file that contains a networkx graph?

推荐答案

鉴于

given what the official docs say, I think you are looking for something like

def read_json_file(filename):
    with open(filename) as f:
        js_graph = json.load(f)
    return json_graph.node_link_graph(js_graph)

即由于json文件是使用json.dump编写的,因此请使用json.load读回内容.

i.e. since the json file is written using json.dump, then use json.load to read the contents back.

然后从加载的字典中创建图形.

Then create the graph from the loaded dictionary.

注意:我从未使用过json_graph软件包,因此我忽略了正确的选项以重新创建特定类型的图形.您可能想在文档中仔细阅读它们,似乎有很多.

Note: I have never used the json_graph package so I ignore what the correct options may be in order to recreate your specific type of graph. You might want to go through them in the docs, there appear to be quite a few.

这篇关于读取json图networkx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆