write_graph6#

write_graph6(G, path, nodes=None, header=True)[source]#

Write a simple undirected graph to a path in graph6 format.

Parameters:
GGraph (undirected)
pathfile or string

File or filename to write. Filenames ending in .gz or .bz2 will be compressed.

nodes: list or iterable

Nodes are labeled 0…n-1 in the order provided. If None the ordering given by G.nodes() is used.

header: bool

If True add ‘>>graph6<<’ string to head of data

Raises:
NetworkXNotImplemented

If the graph is directed or is a multigraph.

ValueError

If the graph has at least 2 ** 36 nodes; the graph6 format is only defined for graphs of order less than 2 ** 36.

Notes

The function writes a newline character after writing the encoding of the graph.

The format does not support edge or node labels, parallel edges or self loops. If self loops are present they are silently ignored.

References

Examples

You can write a graph6 file by giving the path to a file:

>>> import tempfile
>>> with tempfile.NamedTemporaryFile(delete=False) as f:
...     nx.write_graph6(nx.path_graph(2), f.name)
...     _ = f.seek(0)
...     print(f.read())
b'>>graph6<<A_\n'