NetworkX

Previous topic

networkx.read_edgelist

Next topic

networkx.read_weighted_edgelist

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 used to separate values. The default is whitespace.

data : bool or list, optional

If False write no edge data. If True write a string representation of the edge data dictionary.. If a list (or other iterable) is provided, write the keys specified in the list.

Notes

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.

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)
>>> import sys
>>> G=nx.Graph()
>>> G.add_edge(1,2,weight=7,color='red')
>>> nx.write_edgelist(G,sys.stdout,data=False)
1 2
>>> nx.write_edgelist(G,sys.stdout,data=['color'])
1 2 red
>>> nx.write_edgelist(G,sys.stdout,data=['color','weight'])
1 2 red 7