NetworkX

Previous topic

networkx.read_gml

Next topic

networkx.parse_gml

networkx.write_gml

write_gml(G, path)

Write the graph G in GML format to the file or file handle path.

Parameters:

path : filename or filehandle

The filename or filehandle to write. Filenames ending in .gz or .gz2 will be compressed.

See also

read_gml, parse_gml

Notes

The output file will use the default text encoding on your system. It is possible to write files in other encodings by opening the file with the codecs module. See doc/examples/unicode.py for hints.

>>> G=nx.path_graph(4)
>>> import codecs
>>> fh=codecs.open('test.gml','w',encoding='iso8859-1')# use iso8859-1
>>> nx.write_gml(G,fh)

GML specifications indicate that the file should only use 7bit ASCII text encoding.iso8859-1 (latin-1).

Only a single level of attributes for graphs, nodes, and edges, is supported.

Examples

>>> G=nx.path_graph(4)
>>> nx.write_gml(G,"test.gml")

path can be a filehandle or a string with the name of the file.

>>> fh=open("test.gml",'w')
>>> nx.write_gml(G,fh)

Filenames ending in .gz or .bz2 will be compressed.

>>> nx.write_gml(G,"test.gml.gz")