#

Note

This documents the development version of NetworkX. Documentation for the current release can be found here.

#

networkx.readwrite.gml.write_gml

write_gml(G, path, stringizer=None)[source]

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

Parameters
  • G (NetworkX graph) – The graph to be converted to GML.

  • path (filename or filehandle) – The filename or filehandle to write. Files whose names end with .gz or .bz2 will be compressed.

  • stringizer (callable, optional) – A stringizer which converts non-int/non-float/non-dict values into strings. If it cannot convert a value into a string, it should raise a ValueError to indicate that. Default value: None.

Raises

NetworkXError – If stringizer cannot convert a value into a string, or the value to convert is not a string while stringizer is None.

Notes

Graph attributes named ‘directed’, ‘multigraph’, ‘node’ or ‘edge’, node attributes named ‘id’ or ‘label’, edge attributes named ‘source’ or ‘target’ (or ‘key’ if G is a multigraph) are ignored because these attribute names are used to encode the graph structure.

GML files are stored using a 7-bit ASCII encoding with any extended ASCII characters (iso8859-1) appearing as HTML character entities. Without specifying a stringizer/destringizer, the code is capable of handling int/float/str/dict/list data as required by the GML specification. For other data types, you need to explicitly supply a stringizer/destringizer.

Note that while we allow non-standard GML to be read from a file, we make sure to write GML format. In particular, underscores are not allowed in attribute names. For additional documentation on the GML file format, please see the GML url.

See the module docstring networkx.readwrite.gml for more details.

Examples

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

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

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