unable to load a simple csv in networkx in Python

前端 未结 2 901
栀梦
栀梦 2021-01-05 18:58

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 li

2条回答
  •  长发绾君心
    2021-01-05 19:33

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

提交回复
热议问题