read_multiline_adjlist#

read_multiline_adjlist(path, comments='#', delimiter=None, create_using=None, nodetype=None, edgetype=None, encoding='utf-8')[source]#

Read graph in multi-line adjacency list format from path.

Parameters:
pathstring or file

Filename or file handle to read. Filenames ending in .gz or .bz2 will be decompressed.

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.

edgetypePython type, optional

Convert edge data to this type.

commentsstring, optional

Marker for comment lines

delimiterstring, optional

Separator for node labels. The default is whitespace.

Returns:
GNetworkX graph

Notes

This format does not store graph, node, or edge data.

Examples

>>> from pathlib import Path
>>> import tempfile
>>> tmp_path = Path(tempfile.gettempdir())
>>> G = nx.path_graph(4)
>>> fpath = tmp_path / "test.multi_adjlistP4"
>>> nx.write_multiline_adjlist(G, fpath)
>>> H = nx.read_multiline_adjlist(fpath)

Data read from the file are interpreted as strings by default, regardless of the node type of the original graph.

>>> G.edges
EdgeView([(0, 1), (1, 2), (2, 3)])
>>> H.edges
EdgeView([('0', '1'), ('1', '2'), ('2', '3')])

The node data can be converted to a specific type with the nodetype parameter.

>>> H = nx.read_multiline_adjlist(fpath, nodetype=int)
>>> H.edges
EdgeView([(0, 1), (1, 2), (2, 3)])
>>> nx.utils.edges_equal(G.edges, H.edges)
True

Since nodes must be hashable, the function nodetype must return hashable types (e.g. int, float, str, frozenset - or tuples of those, etc.)

The optional create_using parameter indicates the type of NetworkX graph created. The default is nx.Graph, an undirected graph. To read the data as a directed graph use:

>>> H = nx.read_multiline_adjlist(fpath, create_using=nx.DiGraph)
>>> H.is_directed()
True

The path can be a file or a string with the name of the file. If a file is provided, it has to be opened in ‘rb’ mode.

>>> with open(fpath, "rb") as fh:
...     H = nx.read_multiline_adjlist(fh, nodetype=int)
>>> H.edges
EdgeView([(0, 1), (1, 2), (2, 3)])