write_sparse6#

write_sparse6(G, path, nodes=None, header=True)[源代码]#

以sparse6格式将图形G写入给定路径。

参数
G图表(非定向)
path文件或字符串

要写入的文件或文件名

nodes: list or iterable

节点按提供的顺序标记为0...n-1。如果没有,则使用G.Nodes()给出的排序。

header: bool

如果为True,则将稀疏6<<‘字符串添加到数据头

加薪
NetworkXError

如果图是有向的

笔记

该格式不支持边缘或节点标签。

工具书类

1

Sparse6规范<https://users.cecs.anu.edu.au/~bdm/data/formats.html>

实例

您可以通过提供文件路径来写入sparse6文件:

>>> import tempfile
>>> with tempfile.NamedTemporaryFile() 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'