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

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

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

Definition at line 454 of file io.py.

                                                                  :
    """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

    """
    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

    fh=_get_fh(path)

    for line in fh.readlines():
        line = line[:line.find(comments)].strip()
        if not len(line): continue
#        if line.startswith("#") or line.startswith("\n"):
#            continue
#        line=line.strip() #remove trailing \n 
        # split line, should have 2 or three items
        s=line.split(delimiter)
        if len(s)==2:
            (u,v)=s
            d=None
        elif len(s)==3:
            (u,v,d)=s
        else:
            raise "Failed to read line: %s"%line

        # convert types
        try:
            (u,v)=map(nodetype,(u,v))
        except:
            raise TypeError("Failed to convert edge (%s, %s) to type %s"\
                  %(u,v,nodetype))
        if d is not None and edgetype is not None:

            try:
               d=edgetype(d)
            except:
                raise TypeError("Failed to convert edge data (%s) to type %s"\
                                %(d, edgetype))
            
        if xgraph:
            G.add_edge(u,v,d)  # XGraph or XDiGraph
        else:
            G.add_edge(u,v)    # Graph or DiGraph
            
    return G

def write_gpickle(G, path):


Generated by  Doxygen 1.6.0   Back to index