Logo Search packages:      
Sourcecode: python-networkx version File versions  Download package

def networkx::io::read_multiline_adjlist (   path,
  comments = "#",
  delimiter = ' ',
  create_using = None,
  nodetype = None,
  edgetype = None 
)

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)

Definition at line 126 of file io.py.

                                                        :
    """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)
    """
    if create_using is None:
        G=networkx.Graph()
    else:
        try:
            G=create_using
            G.clear()
        except:
            raise TypeError("Input graph is not a networkx graph type")

    # is this a XGraph or XDiGraph?
    if hasattr(G,'allow_multiedges')==True:
        xgraph=True
    else:
        xgraph=False

    inp=_get_fh(path)        

    for line in inp:
#        if line.startswith("#") or line.startswith("\n"):
#            continue
#        line=line.strip() #remove trailing \n 
        line = line[:line.find(comments)].strip()
        if not len(line): continue
        try:
            (u,deg)=line.split(delimiter)
            deg=int(deg)
        except:
            raise "Failed to read node and degree on line (%s)"%line
        try:
            if nodetype is not None:
                u=nodetype(u)
        except:
            raise TypeError("Failed to convert node (%s) to type %s"\
                  %(u,nodetype))
        G.add_node(u)
        for i in range(deg):
            line=inp.next().strip()
            vlist=line.split(delimiter)
            if len(vlist)==1:
                v=vlist[0]
                d=None
            elif len(vlist)==2:
                (v,d)=vlist
            else:
                raise "Failed to read line: %s"%vlist
            try:
                if nodetype is not None:
                    v=nodetype(v)
            except:
                raise TypeError("Failed to convert node (%s) to type %s"\
                                %(v,nodetype))
            if xgraph:
                if d is not None:
                    try:
                        if edgetype is not None:
                            d=edgetype(d)
                    except:
                        raise TypeError\
                              ("Failed to convert edge data (%s) to type %s"\
                                %(d, edgetype))
                G.add_edge(u,v,d)
            else:
                G.add_edge(u,v)
                
    return G


def write_adjlist(G, path, comments="#", delimiter=' '):


Generated by  Doxygen 1.6.0   Back to index