networkx.readwrite.edgelist.parse_edgelist

parse_edgelist(lines, comments='#', delimiter=None, create_using=None, nodetype=None, data=True)[source]

Parse lines of an edge list representation of a graph.

Parameters
lineslist or iterator of strings

Input data in edgelist format

commentsstring, optional

Marker for comment lines. Default is '#'

delimiterstring, optional

Separator for node labels. Default is None, meaning any whitespace.

create_usingNetworkX graph constructor, optional (default=nx.Graph)

Graph type to create. If graph instance, then cleared before populated.

nodetypePython type, optional

Convert nodes to this type. Default is None, meaning no conversion is performed.

databool or list of (label,type) tuples

If False generate no edge data or if True use a dictionary representation of edge data or a list tuples specifying dictionary key names and types for edge data.

Returns
G: NetworkX Graph

The graph corresponding to lines

Examples

Edgelist with no data:

>>> lines = ["1 2", "2 3", "3 4"]
>>> G = nx.parse_edgelist(lines, nodetype=int)
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges())
[(1, 2), (2, 3), (3, 4)]

Edgelist with data in Python dictionary representation:

>>> lines = ["1 2 {'weight': 3}", "2 3 {'weight': 27}", "3 4 {'weight': 3.0}"]
>>> G = nx.parse_edgelist(lines, nodetype=int)
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges(data=True))
[(1, 2, {'weight': 3}), (2, 3, {'weight': 27}), (3, 4, {'weight': 3.0})]

Edgelist with data in a list:

>>> lines = ["1 2 3", "2 3 27", "3 4 3.0"]
>>> G = nx.parse_edgelist(lines, nodetype=int, data=(("weight", float),))
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges(data=True))
[(1, 2, {'weight': 3.0}), (2, 3, {'weight': 27.0}), (3, 4, {'weight': 3.0})]