NetworkX

Previous topic

Adjacency List

Next topic

networkx.write_adjlist

Quick search

networkx.read_adjlist

read_adjlist(path, comments='#', delimiter=' ', create_using=None, nodetype=None)

Read graph in single line adjacency list format from path.

Examples

>>> G=nx.path_graph(4)
>>> nx.write_adjlist(G, "test.adjlist")
>>> G=nx.read_adjlist("test.adjlist")

path can be a filehandle or a string with the name of the file.

>>> fh=open("test.adjlist")
>>> G=nx.read_adjlist(fh)

Filenames ending in .gz or .bz2 will be compressed.

>>> nx.write_adjlist(G, "test.adjlist.gz")
>>> G=nx.read_adjlist("test.adjlist.gz")

nodetype is an optional function to convert node strings to nodetype

For example

>>> G=nx.read_adjlist("test.adjlist", nodetype=int)

will attempt to convert all nodes to integer type

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

create_using is an optional networkx graph type, the default is Graph(), an undirected graph.

>>> G=nx.read_adjlist("test.adjlist", create_using=nx.DiGraph())

Does not handle edge data: use ‘read_edgelist’ or ‘read_multiline_adjlist’

The comments character (default=’#’) at the beginning of a line indicates a comment line.

The entries are separated by delimiter (default=’ ‘). If whitespace is significant in node or edge labels you should use some other delimiter such as a tab or other symbol.

Sample format:

# source target
a b c
d e