read_sparse6#
- read_sparse6(path)[源码]#
从 path 中读取 sparse6 格式的无向图。
- 参数:
- path文件或字符串
要写入的文件或文件名。
- 返回:
- G图/多重图 或 图/多重图的列表
如果文件包含多行,则返回一个图列表
- 抛出:
- NetworkXError
如果字符串无法解析为 sparse6 格式
参考文献
[1]Sparse6 规范 <https://users.cecs.anu.edu.au/~bdm/data/formats.html>
示例
可以通过提供文件路径来读取 sparse6 文件
>>> import tempfile >>> with tempfile.NamedTemporaryFile(delete=False) as f: ... _ = f.write(b">>sparse6<<:An\n") ... _ = f.seek(0) ... G = nx.read_sparse6(f.name) >>> list(G.edges()) [(0, 1)]
也可以通过提供一个已打开的类文件对象来读取 sparse6 文件
>>> import tempfile >>> with tempfile.NamedTemporaryFile() as f: ... _ = f.write(b">>sparse6<<:An\n") ... _ = f.seek(0) ... G = nx.read_sparse6(f) >>> list(G.edges()) [(0, 1)]