read_edgelist#
- read_edgelist(path, comments='#', delimiter=None, create_using=None, nodetype=None, data=True, edgetype=None, encoding='utf-8')[source]#
Read a graph from a list of edges.
- Parameters:
- pathfile or string
File or filename to read. If a file is provided, it must be opened in ‘rb’ mode. Filenames ending in .gz or .bz2 will be decompressed.
- commentsstring, optional
The character used to indicate the start of a comment. To specify that no character should be treated as a comment, use
comments=None.- delimiterstring, optional
The string used to separate values. The default is whitespace.
- create_usingNetworkX graph constructor, optional (default=nx.Graph)
Graph type to create. If graph instance, then cleared before populated.
- nodetypeint, float, str, Python type, optional
Convert node data from strings to specified type
- databool or list of (label,type) tuples
Tuples specifying dictionary key names and types for edge data
- edgetypeint, float, str, Python type, optional OBSOLETE
Convert edge data from strings to specified type and use as ‘weight’
- encoding: string, optional
Specify which encoding to use when reading file.
- Returns:
- Ggraph
A networkx Graph or other type specified with create_using
See also
Notes
Since nodes must be hashable, the function nodetype must return hashable types (e.g. int, float, str, frozenset - or tuples of those, etc.)
Examples
>>> from pathlib import Path >>> import tempfile >>> tmp_path = Path(tempfile.gettempdir())
>>> fpath = tmp_path / "P4.edgelist" >>> nx.write_edgelist(nx.path_graph(4), fpath) >>> G = nx.read_edgelist(fpath) >>> G.edges EdgeView([('0', '1'), ('1', '2'), ('2', '3')])
Nodes read from the file are interpreted as strings by default. The
nodetypeparameter can be used to convert the nodes to another type:>>> G = nx.read_edgelist(fpath, nodetype=int) >>> G.edges EdgeView([(0, 1), (1, 2), (2, 3)])
The optional
create_usingparameter indicates the type of NetworkX graph created. By default an undirectednx.Graphis returned. To read the data as a directed graph use:>>> G = nx.read_edgelist(fpath, create_using=nx.DiGraph) >>> G.is_directed() True
By default, edge data is expected to be stored in a serialized dict-like format keyed by the attribute name:
>>> fpath = tmp_path / "test.text" >>> with open(fpath, "w") as fh: # Create a file with data in default format ... _ = fh.write("1 2 {'weight': 3}")
>>> G = nx.read_edgelist(fpath, nodetype=int) >>> G.edges(data=True) EdgeDataView([(1, 2, {'weight': 3})])
Alternatively, edge data stored as a flat list without keys can be parsed by passing in the attribute name and data type via
data:>>> textline = "1 2 red 10" >>> fpath = tmp_path / "test.textline" >>> with open(fpath, "w") as fh: # Create a file with flattened edge data ... _ = fh.write(textline)
>>> G = nx.read_edgelist( ... fpath, nodetype=int, data=[("color", str), ("weight", float)] ... ) >>> G.edges(data=True) EdgeDataView([(1, 2, {'color': 'red', 'weight': 10.0})])
See
parse_edgelistfor more examples of formatting.