read_edgelist#
- read_edgelist(path, comments='#', delimiter=None, create_using=None, nodetype=None, data=True, edgetype=None, encoding='utf-8')[source]#
Read a bipartite 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.
- delimiterstring, optional
The string used to separate values. The default is whitespace.
- create_usingGraph container, optional,
Use specified container to build graph. The default is networkx.Graph, an undirected graph.
- 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 / "test.edgelist" >>> G = nx.path_graph(4) >>> G.add_nodes_from([0, 2], bipartite=0) >>> G.add_nodes_from([1, 3], bipartite=1) >>> nx.bipartite.write_edgelist(G, fpath)
>>> H = nx.bipartite.read_edgelist(fpath) >>> H.nodes(data="bipartite") NodeDataView({'0': 0, '1': 1, '2': 0, '3': 1}, data='bipartite') >>> H.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:>>> H = nx.bipartite.read_edgelist(fpath, nodetype=int) >>> H.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:>>> H = nx.bipartite.read_edgelist(fpath, create_using=nx.DiGraph) >>> H.is_directed() True
By default, edge data is expected to be stored in a serialized dict-like format keyed by attribute name:
>>> fpath = tmp_path / "test_edgelist.data" >>> with open(fpath, "w") as fh: ... _ = fh.write("1 2 {'weight': 3}")
>>> G = nx.bipartite.read_edgelist(fpath, nodetype=int) >>> G.nodes(data="bipartite") NodeDataView({1: 0, 2: 1}, data='bipartite') >>> 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_edgelist.multidata" >>> with open(fpath, "w") as fh: # Create a file with multi edge data attrs ... _ = fh.write(textline)
>>> G = nx.bipartite.read_edgelist( ... fpath, nodetype=int, data=[("color", str), ("weight", float)] ... ) >>> G.nodes(data="bipartite") NodeDataView({1: 0, 2: 1}, data='bipartite') >>> G.edges(data=True) EdgeDataView([(1, 2, {'color': 'red', 'weight': 10.0})])