读取图数据

边列表

graphscope.nx.read_edgelist(path, comments='#', delimiter=None, create_using=None, nodetype=None, data=True, edgetype=None, encoding='utf-8')[源代码]

从边列表中读取图数据。

Parameters:
  • path (file or string) – 要读取的文件或文件名。如果提供了文件对象,必须以'rb'模式打开。 以.gz或.bz2结尾的文件名将被自动解压。

  • comments (string, optional) – 用于标识注释起始的字符。若要指定不应将任何字符视为注释,请使用comments=None

  • delimiter (string, optional) – 用于分隔值的字符串。默认为空格。

  • create_using (NetworkX 图构造函数可选 (默认=nx.Graph)) - 要创建的图类型。如果是图实例,则在填充前会被清空。

  • nodetype (int, float, str, Python type, optional) - 将节点数据从字符串转换为指定类型

  • data (boollist 包含 (label,type) 元组) – 用于指定边数据的字典键名和类型的元组

  • edgetype (int, float, str, Python type, optional OBSOLETE) - 将边数据从字符串转换为指定类型并用作'weight'

  • encoding (string, optional) – 指定读取文件时使用的编码格式。

Returns:

G – 一个networkx图或其他通过create_using指定的类型

Return type:

示例

>>> nx.write_edgelist(nx.path_graph(4), "test.edgelist")
>>> G = nx.read_edgelist("test.edgelist")
>>> fh = open("test.edgelist", "rb")
>>> G = nx.read_edgelist(fh)
>>> fh.close()
>>> G = nx.read_edgelist("test.edgelist", nodetype=int)
>>> G = nx.read_edgelist("test.edgelist", create_using=nx.DiGraph)

包含列表数据的边列表:

>>> textline = "1 2 3"
>>> fh = open("test.edgelist", "w")
>>> d = fh.write(textline)
>>> fh.close()
>>> G = nx.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()。

另请参阅

parse_edgelist, write_edgelist

备注

由于节点必须是可哈希的,函数nodetype必须返回可哈希的类型(例如int、float、str、frozenset - 或这些类型的元组等)

邻接表

graphscope.nx.read_adjlist(path, comments='#', delimiter=None, create_using=None, nodetype=None, encoding='utf-8')[源代码]

从路径读取邻接表格式的图数据。

Parameters:
  • path (string or file) – 要读取的文件名或文件句柄。以.gz或.bz2结尾的文件名将被解压缩。

  • create_using (graphscope.nx 图构造函数, 可选 (默认=nx.Graph)) - 要创建的图类型。如果是图实例,则会在填充前被清空。

  • nodetype (int, str, float, tuple, bool Python object, optional) – 将节点转换为此类型。

  • comments (string, optional) – 注释行的标记

  • delimiter (string, optional) – 节点标签的分隔符。默认为空格。

Returns:

G – 对应邻接表格式中线条的图。

Return type:

graphscope.nx 图

备注

此格式不存储图或节点数据。

另请参阅

read_edgelist