Package networkx :: Module io
[hide private]
[frames] | no frames]

Module io

source code

Read and write NetworkX graphs.

Note that NetworkX graphs can contain any hashable Python object as node (not just integers and strings). So writing a NetworkX graph as a text file may not always be what you want: see write_gpickle and gread_gpickle for that case.

This module provides the following :

Edgelist format: Useful for connected graphs with or without edge data.

write_edgelist(G, path) G=read_edgelist(path)

Adjacency list with single line per node: Useful for connected or unconnected graphs without edge data.

write_adjlist(G, path) G=read_adjlist(path)

Adjacency list with multiple lines per node: Useful for connected or unconnected graphs with or without edge data.

write_multiline_adjlist(G, path) read_multiline_adjlist(path)

Python pickled format: Useful for graphs with non text representable data.

write_gpickle(G, path) read_gpickle(path)



Date:  

Author: Aric Hagberg (hagberg@lanl.gov) Dan Schult (dschult@colgate.edu)

Functions [hide private]
 
write_multiline_adjlist(G, path, delimiter=' ', comments='#')
Write the graph G in multiline adjacency list format to the file or file handle path.
source code
 
read_multiline_adjlist(path, comments='#', delimiter=' ', create_using=None, nodetype=None, edgetype=None)
Read graph in multi-line adjacency list format from path.
source code
 
write_adjlist(G, path, comments='#', delimiter=' ')
Write graph G in single-line adjacency-list format to path.
source code
 
read_adjlist(path, comments='#', delimiter=' ', create_using=None, nodetype=None)
Read graph in single line adjacency list format from path.
source code
 
write_edgelist(G, path, comments='#', delimiter=' ')
Write graph G in edgelist format on file path.
source code
 
read_edgelist(path, comments='#', delimiter=' ', create_using=None, nodetype=None, edgetype=None)
Read graph in edgelist format from path.
source code
 
write_gpickle(G, path)
Write graph object in Python pickle format.
source code
 
read_gpickle(path)
Read graph object in Python pickle format
source code
 
write_yaml(G, path, default_flow_style=False, **kwds)
Write graph G in YAML text format to path.
source code
 
read_yaml(path)
Read graph from YAML format from path.
source code
 
_get_fh(path, mode='r')
Return a file handle for given path.
source code
 
_test_suite() source code
Variables [hide private]
  __credits__ = ''
  __revision__ = '$$'
Function Details [hide private]

write_multiline_adjlist(G, path, delimiter=' ', comments='#')

source code 

Write the graph G in multiline adjacency list format to the file or file handle path.

See read_multiline_adjlist for file format details.

>>> write_multiline_adjlist(G,"file.adjlist")

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

>>> fh=open("file.adjlist")
>>> write_multiline_adjlist(G,fh)

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

>>> write_multiline_adjlist(G,"file.adjlist.gz")

The file will use the default text encoding on your system. It is possible to write files in other encodings by opening the file with the codecs module. See doc/examples/unicode.py for hints.

>>> import codecs
>>> fh=codecs.open("file.adjlist",encoding='utf=8') # use utf-8 encoding
>>> write_multiline_adjlist(G,fh)

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

source code 

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

>>> G=read_multiline_adjlist("file.adjlist")

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

>>> fh=open("file.adjlist")
>>> G=read_multiline_adjlist(fh)

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

>>> G=read_multiline_adjlist("file.adjlist.gz")

nodetype is an optional function to convert node strings to nodetype

For example

>>> G=read_multiline_adjlist("file.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.)

edgetype is a function to convert edge data strings to edgetype

>>> G=read_multiline_adjlist("file.adjlist", edgetype=int)

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

>>> G=read_multiline_adjlist("file.adjlist", create_using=DiGraph())

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.

Example multiline adjlist file format:

# source target for Graph or DiGraph
a 2
b
c
d 1
e

or

# source target for XGraph or XDiGraph with edge data a 2 b edge-ab-data c edge-ac-data d 1 e edge-de-data

Reading the file will use the default text encoding on your system. It is possible to read files with other encodings by opening the file with the codecs module. See doc/examples/unicode.py for hints.

>>> import codecs
>>> fh=codecs.open("file.adjlist", encoding='utf=8') # use utf-8 encoding
>>> G=read_multiline_adjlist(fh)

write_adjlist(G, path, comments='#', delimiter=' ')

source code 

Write graph G in single-line adjacency-list format to path.

See read_adjlist for file format details.

>>> write_adjlist(G, "file.adjlist")

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

>>> fh=open("file.adjlist")
>>> write_adjlist(G, fh)

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

>>> write_adjlist(G, "file.adjlist.gz")

The file will use the default text encoding on your system. It is possible to write files in other encodings by opening the file with the codecs module. See doc/examples/unicode.py for hints.

>>> import codecs
>>> fh=codecs.open("file.adjlist",encoding='utf=8') # use utf-8 encoding
>>> write_adjlist(G,fh)

Does not handle data in XGraph or XDiGraph, use 'write_edgelist' or 'write_multiline_adjlist'

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

source code 

Read graph in single line adjacency list format from path.

>>> G=read_adjlist("file.adjlist")

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

>>> fh=open("file.adjlist")
>>> G=read_adjlist(fh)

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

>>> G=read_adjlist("file.adjlist.gz")

nodetype is an optional function to convert node strings to nodetype

For example

>>> G=read_adjlist("file.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(), a simple undirected graph

>>> G=read_adjlist("file.adjlist", create_using=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.

# source target a b c d e

write_edgelist(G, path, comments='#', delimiter=' ')

source code 

Write graph G in edgelist format on file path.

See read_edgelist for file format details.

>>> write_edgelist(G, "file.edgelist")

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

>>> fh=open("file.edgelist")
>>> write_edgelist(G,fh)

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

>>> write_edgelist(G, "file.edgelist.gz")

The file will use the default text encoding on your system. It is possible to write files in other encodings by opening the file with the codecs module. See doc/examples/unicode.py for hints.

>>> import codecs
>>> fh=codecs.open("file.edgelist",encoding='utf=8') # use utf-8 encoding
>>> write_edgelist(G,fh)

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

source code 

Read graph in edgelist format from path.

>>> G=read_edgelist("file.edgelist")

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

>>> fh=open("file.edgelist")
>>> G=read_edgelist(fh)

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

>>> G=read_edgelist("file.edgelist.gz")

nodetype is an optional function to convert node strings to nodetype

For example

>>> G=read_edgelist("file.edgelist", 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(), a simple undirected graph

>>> G=read_edgelist("file.edgelist",create_using=DiGraph())

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.

Example edgelist file format:

# source target
a b
a c
d e

or for an XGraph() with edge data

# source target data a b 1 a c 3.14159 d e apple

write_gpickle(G, path)

source code 

Write graph object in Python pickle format.

This will preserve Python objects used as nodes or edges.

>>> write_gpickle(G,"file.gpickle")

See cPickle.

read_gpickle(path)

source code 

Read graph object in Python pickle format

>>> G=read_gpickle("file.gpickle")

See cPickle.

write_yaml(G, path, default_flow_style=False, **kwds)

source code 

Write graph G in YAML text format to path.

See http://www.yaml.org

read_yaml(path)

source code 

Read graph from YAML format from path.

See http://www.yaml.org

_get_fh(path, mode='r')

source code 

Return a file handle for given path.

Path can be a string or a file handle.

Attempt to uncompress/compress files ending in '.gz' and '.bz2'.