Danny
Danny

Reputation: 325

Read csv edgelist into networkx

I have a dataset in the format of:

1,2

2,3

1,3

etc. (each pair represents an edge between the two nodes, e.g. '1,2' is an edge between node 1 and node 2)

I need to read this into networkx. Currently I'm trying to read it in as a list where each pair is one element in the list, but that isn't working.

Upvotes: 2

Views: 9008

Answers (1)

Aric
Aric

Reputation: 25289

You can use networkx.read_edgelist(file, delimeter=',').

e.g.

import StringIO
import networkx as nx
data = StringIO.StringIO("""1,2

2,3

1,3
""")

G = nx.read_edgelist(data, delimiter=',', nodetype=str)
for e in G.edges():
    print e
# ('1', '3')
# ('1', '2')
# ('3', '2')

Upvotes: 6

Related Questions