cfpq_data.graphs.readwrite.csv#

Read (and write) a graph from (and to) CSV file.

Functions

graph_from_csv(path)

Loads a graph from CSV file.

graph_to_csv(graph, path)

Saves the graph to the CSV file by path.

graph_from_csv(path: Path | str) → MultiDiGraph[source]#

Loads a graph from CSV file.

Parameters:
pathUnion[Path, str]

The path to the CSV file with which the graph will be created.

Returns:
gMultiDiGraph

Loaded graph.

Examples

>>> from cfpq_data import *
>>> import pathlib, tempfile
>>> p = pathlib.Path(tempfile.mkdtemp()) / "g.csv"
>>> _ = p.write_text("0 1 a\n1 2 b\n")
>>> g = graph_from_csv(p)
>>> g.number_of_nodes()
3
>>> g.number_of_edges()
2
graph_to_csv(graph: MultiDiGraph, path: Path | str) → Path[source]#

Saves the graph to the CSV file by path.

Parameters:
graphMultiDiGraph

Graph to save.

pathUnion[Path, str]

The path to the CSV file where the graph will be saved.

Returns:
pathPath

Path to the CSV file where the graph will be saved.

Examples

>>> from cfpq_data import *
>>> import pathlib, tempfile
>>> d = pathlib.Path(tempfile.mkdtemp())
>>> p = d / "g.csv"
>>> _ = p.write_text("0 1 a\n1 2 b\n")
>>> g = graph_from_csv(p)
>>> path = graph_to_csv(g, d / "out.csv")
>>> g2 = graph_from_csv(path)
>>> sorted((u, v, e["label"]) for u, v, e in g2.edges(data=True)) == \
...     sorted((u, v, e["label"]) for u, v, e in g.edges(data=True))
True