NetworkX

Previous topic

networkx.read_edgelist

Next topic

GML

networkx.write_edgelist

write_edgelist(G, path, comments='#', delimiter=' ', data=True)

Write graph as a list of edges.

Parameters:

G : graph

A NetworkX graph

path : file or string

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

comments : string, optional

The character used to indicate the start of a comment

delimiter : string, optional

The string uses to separate values. The default is whitespace.

data : bool, optional

If True write a string representation of the edge data.

Notes

With data=True each line will have three string values: the string representation of the source, target, and edge data.

The 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.edgelist",'w',encoding='utf=8') # utf-8 encoding
>>> nx.write_edgelist(G,fh)

Examples

>>> G=nx.path_graph(4)
>>> nx.write_edgelist(G, "test.edgelist")
>>> G=nx.path_graph(4)
>>> fh=open("test.edgelist",'w')
>>> nx.write_edgelist(G, fh)
>>> nx.write_edgelist(G, "test.edgelist.gz")
>>> nx.write_edgelist(G, "test.edgelist.gz", data=False)