This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.


write_gml(G, path)[source]

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


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

This implementation does not support all Python data types as GML data. Nodes, node attributes, edge attributes, and graph attributes must be either dictionaries or single stings or numbers. If they are not an attempt is made to represent them as strings. For example, a list as edge data G[1][2][‘somedata’]=[1,2,3], will be represented in the GML file as:

edge [
  source 1
  target 2
  somedata "[1, 2, 3]"


>>> 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")