How to create a graph in Python using a CSV File data by graph-tool?
<p>I'm trying to create a graph with graph-tool (<a href="https://graph-tool.skewed.de" rel="nofollow">https://graph-tool.skewed.de</a>) from csv file that content like:</p>

<pre><code>A,B,50
A,C,34
C,D,55
D,D,80
A,D,90
B,D,78
</code></pre>

<p>Now I want to create a graph with A, B, C, D as nodes and the third column numbers as edges. I am using graph-tool library. The third column number shows the common items shared by A,B and A,C and so on.</p>

<p>I can do it by "networkx" (read_edgelist and etc) but I want to do it with graph-tool.</p>

<p>Assuming you already know how to read the CSV file in Python (for example, using the <a href="https://docs.python.org/3.6/library/csv.html" rel="nofollow">CSV library</a>), The docs on the website <a href="https://graph-tool.skewed.de/static/doc/quickstart.html#creating-and-manipulating-graphs" rel="nofollow">explain how to do this very clearly.</a></p>

<p>Something like</p>

<pre><code>import graph_tool
g = Graph(directed=False)


# this is the result of csv.parse(file)
list_of_edges = [['A', 'B', 50], ['A','C',34], ['C','D',55], ['D','D',80], ['A','D',90], ['B','D',78]]

vertices = {}

for e in list_of_edges:
    if e[0] not in vertices:
        vertices[e[0]] = True
    if e[1] not in vertices:
        vertices[e[1]] = True


for d in vertices:
    vertices[d] = g.add_vertex()

for edge in list_of_edges:
    g.add_edge(vertices[edge[0]], vertices[edge[1]])
</code></pre>

python csv graph-tool