unable to load a simple csv in networkx in Python

前端 未结 2 900
栀梦
栀梦 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:29

    file is a python keyword. Try naming the variable something else like csvfile.

    0 讨论(0)
  • 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")
    
    0 讨论(0)
提交回复
热议问题