Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
931 views
in Technique[技术] by (71.8m points)

unable to load a simple csv in networkx in Python

I am a complete noobie in Python, and I would like to study a dataset using the networkx package. I do not understand what is wrong here:

I have a csv which looks like this (extract):

['152027', '-6167']
['152027', '-4982']
['152027', '-3810']
['152027', '-2288']
['152027', '-1253']
['152100', '-152100']
['152100', '-86127']

lets call this .csv file nodes. Numbers heres have no particular meanings. They are just anonymised names: so 152027 is a person that is connected to individual -6167, individual -4982, etc.

I use the following code in Python

import csv
import networkx as nx

file = csv.reader(open('nodes', 'rb'), delimiter=',')

G=nx.read_edgelist(file, delimiter=',',nodetype=float,encoding='utf-8')
G.number_of_nodes()

and I get the sad Out[71]: 0 I do not understand what is wrong here. Could you please help me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

nx.read_edgelist expects the first variable to be a file handle or filename string, not a csv.reader object.

Don't use csv at all; try just

G = nx.read_edgelist('nodes', delimiter=',', nodetype=int, encoding="utf-8")

Edit: if you need to skip a header line, you could do

with open('nodes', 'rb') as inf:
    next(inf, '')   # skip a line
    G = nx.read_edgelist(inf, delimiter=',', nodetype=int, encoding="utf-8")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...