简体   繁体   English

将csv edgelist读入networkx

[英]Read csv edgelist into networkx

I have a dataset in the format of: 我有一个格式为的数据集:

1,2 1,2

2,3 2,3

1,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) 等(每对代表两个节点之间的边缘,例如'1,2'是节点1和节点2之间的边缘)

I need to read this into networkx. 我需要将其读入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=','). 您可以使用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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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