简体   繁体   中英

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, eg '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.

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

eg

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM