write_sparse6#
- write_sparse6(G, path, nodes=None, header=True)[源码]#
将图 G 以 sparse6 格式写入给定路径。
- 参数:
- G图 (无向)
- path文件或字符串
要写入的文件或文件名
- nodes: list 或 iterable
节点按照提供的顺序标记为 0…n-1。如果为 None,则使用 G.nodes() 给定的顺序。
- header: bool
如果为 True,则在数据开头添加字符串 ‘>>sparse6<<’
- 引发:
- NetworkXError
如果图是有向图
注意
该格式不支持边或节点标签。
参考资料
[1]Sparse6 规范 <https://users.cecs.anu.edu.au/~bdm/data/formats.html>
示例
可以通过提供文件路径来写入 sparse6 文件
>>> import tempfile >>> with tempfile.NamedTemporaryFile(delete=False) as f: ... nx.write_sparse6(nx.path_graph(2), f.name) ... print(f.read()) b'>>sparse6<<:An\n'
也可以通过提供一个已打开的类文件对象来写入 sparse6 文件
>>> with tempfile.NamedTemporaryFile() as f: ... nx.write_sparse6(nx.path_graph(2), f) ... _ = f.seek(0) ... print(f.read()) b'>>sparse6<<:An\n'