write_sparse6#
- write_sparse6(G, path, nodes=None, header=True)[source]#
将图 G 以 sparse6 格式写入指定路径。
- Parameters:
- G图(无向)
- path文件或字符串
要写入的文件或文件名
- nodes: 列表或可迭代对象
节点按提供的顺序标记为 0…n-1。如果为 None,则使用 G.nodes() 给出的顺序。
- header: 布尔值
如果为 True,则在数据头部添加 ‘>>sparse6<<’ 字符串
- Raises:
- NetworkXError
如果图是有向的
See also
Notes
该格式不支持边或节点标签。
References
[1]Sparse6 规范 <https://users.cecs.anu.edu.au/~bdm/data/formats.html>
Examples
你可以通过提供文件路径来写入 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
‘
你也可以通过提供一个打开的类文件对象来写入 sparse6 文件:
>>> with tempfile.NamedTemporaryFile() as f: ... nx.write_sparse6(nx.path_graph(2), f) ... _ = f.seek(0) ... print(f.read()) b'>>sparse6<<:An
‘