Skip to content

Reading and writing graphs

igraph_ctypes.io module

write_graph_edgelist(graph, outstream)

Writes the graph in plain edge list format to an output stream.

The plain edge list format records the structure of the graph only and the vertices of the graph will be referred to as numeric vertex IDs instead of vertex names.

See write_graph_ncol() if you have vertex names and you want to use them in the output file instead of IDs.

Parameters:

Name Type Description Default
graph Graph

the graph to write

required
outstream FileLike

the output file or stream to write the graph to. May be a filename, a path-like object or a file-like object if it is backed by a low-level file handle

required

write_graph_graphml(graph, outstream, prefixattr=True)

Writes the graph in GraphML format to an output stream.

The GraphML format preserves numeric, string and boolean attributes.

Parameters:

Name Type Description Default
graph Graph

the graph to write

required
outstream FileLike

the output file or stream to write the graph to. May be a filename, a path-like object or a file-like object if it is backed by a low-level file handle

required
prefixattr bool

whether to put a prefix in front of the attribute names to ensure uniqueness if the graph has vertex and edge (or graph) attributes with the same name

True

write_graph_lgl(graph, outstream, names='name', weights='weight', isolates=True)

Writes the graph in the LGL (Large Graph Layout) format to an output stream.

The LGL format can store the structure of a graph with named vertices and weighted edges.

Note that the file format does not store whether the graph is directed or not; this information has to be supplied when the graph is read back.

Parameters:

Name Type Description Default
graph Graph

the graph to write

required
outstream FileLike

the output file or stream to write the graph to. May be a filename, a path-like object or a file-like object if it is backed by a low-level file handle

required
names str

name of the string vertex attribute that stores the names of the vertices to be written into the output file. A warning will be thrown if the attribute does not exist or is not a string attribute, and the file will contain numeric vertex IDs instead if this is the case.

'name'
weights str

name of the edge attribute that stores the weights of the edges to be written into the output file. A warning will be thrown if the attribute does not exist or is not a numeric attribute, and all weights will be assumed to be equal to 1 if this is the case.

'weight'
isolates bool

whether to save isolated vertices to the output

True

write_graph_ncol(graph, outstream, names='name', weights='weight')

Writes the graph in the NCOL format to an output stream.

The NCOL format is essentially a two- or three-column named edge list format. Each line in the output corresponds to an edge. The first two columns contain the names of the source and target vertices of an edge. THe third column (if exists) contains the weight of each edge.

Note that the file format does not store whether the graph is directed or not; this information has to be supplied when the graph is read back.

Parameters:

Name Type Description Default
graph Graph

the graph to write

required
outstream FileLike

the output file or stream to write the graph to. May be a filename, a path-like object or a file-like object if it is backed by a low-level file handle

required
names str

name of the string vertex attribute that stores the names of the vertices to be written into the output file. A warning will be thrown if the attribute does not exist or is not a string attribute, and the file will contain numeric vertex IDs instead if this is the case.

'name'
weights str

name of the edge attribute that stores the weights of the edges to be written into the output file. A warning will be thrown if the attribute does not exist or is not a numeric attribute, and all weights will be assumed to be equal to 1 if this is the case.

'weight'