read_edgelist#
- read_edgelist(path, comments='#', delimiter=None, create_using=None, nodetype=None, data=True, edgetype=None, encoding='utf-8')[源代码]#
从边列表中读取二部图。
- 参数
- path文件或字符串
要读取的文件或文件名。如果提供了文件,则必须在RB模式下打开该文件。将解压缩以.gz或.bz2结尾的文件名。
- comments字符串,可选
用于指示注释开始的字符。
- delimiter字符串,可选
用于分隔值的字符串。默认为空格。
- create_using图形容器,可选,
使用指定的容器构建图形。缺省值为networkx.Graph,即无向图。
- nodetypeInt、Float、str、Python类型,可选
将节点数据从字符串转换为指定类型
- data布尔或(标签、类型)元组列表
为边数据指定字典关键字名称和类型的元组
- edgetypeInt、浮点数、字符串、Python型、可选过时
将边缘数据从字符串转换为指定类型并用作‘权重’
- encoding: string, optional
指定读取文件时要使用的编码。
- 返回
- G图表
使用CREATE_USING指定的网络X图或其他类型
笔记
由于节点必须是可哈希的,因此函数nodeType必须返回可哈希类型(例如int、float、str、frozenset-或这些类型的元组等)。
实例
>>> from networkx.algorithms import bipartite >>> G = nx.path_graph(4) >>> G.add_nodes_from([0, 2], bipartite=0) >>> G.add_nodes_from([1, 3], bipartite=1) >>> bipartite.write_edgelist(G, "test.edgelist") >>> G = bipartite.read_edgelist("test.edgelist")
>>> fh = open("test.edgelist", "rb") >>> G = bipartite.read_edgelist(fh) >>> fh.close()
>>> G = bipartite.read_edgelist("test.edgelist", nodetype=int)
数据在列表中的EdgeList:
>>> textline = "1 2 3" >>> fh = open("test.edgelist", "w") >>> d = fh.write(textline) >>> fh.close() >>> G = bipartite.read_edgelist( ... "test.edgelist", nodetype=int, data=(("weight", float),) ... ) >>> list(G) [1, 2] >>> list(G.edges(data=True)) [(1, 2, {'weight': 3.0})]
有关格式化的更多示例,请参阅parse_edgelist()。