Unable to load simple csv in networkx in Python

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

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

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

allows you to call this CSV file nodes. Numbers have no special meaning. These are just anonymous names: for example, 152027 people who are associated with an individual -6167, individual -4982, etc.

I am using 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 sad Out[71]: 0 I don’t understand what is wrong here. Could you help me?

+4
source share
2 answers

nx.read_edgelist , , csv.reader.

csv ;

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

:, ,

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

file - python. - , csvfile.

0

Source: https://habr.com/ru/post/1534303/


All Articles