Python 无法将csv文件加载到networkx

Python 无法将csv文件加载到networkx,python,networkx,Python,Networkx,我有一个具有标题和行的数据集,采用这种格式 login,project_name,ccount kts,blocklist-ipsets,2192232 Ken,heartbeat,1477010 Code,feature-tests,584334 dat,mirage-ci-logs,584046 vip,committer,533839 RQHVMCAU,mirror,505976 ANHTMOTA,d43-en,378663 mikins,openapsdev,348992 direwo

我有一个具有标题和行的数据集,采用这种格式

login,project_name,ccount

kts,blocklist-ipsets,2192232
Ken,heartbeat,1477010
Code,feature-tests,584334
dat,mirage-ci-logs,584046
vip,committer,533839
RQHVMCAU,mirror,505976
ANHTMOTA,d43-en,378663
mikins,openapsdev,348992
direwolf-github,my-app,340046
其中login、project_name和ccount是标题
如何将此数据集读入networkx图形并查看图形。ccount值是边的权重。我是python和networkx的新手。
这是我们已经做过的

import networkx as nx
import csv
def _get_graph_file():
   G = nx.DiGraph()
   git = csv.reader('file.csv')
   G.add_weighted_edges_from(git_df)
   return G
print(_get_graph_file())

您需要将文件对象传递给csv读取器,而不是直接传递文件。此外,用于添加具有边权重的节点的语法不正确。您使用的变量名不正确

以下是具有正确语法和格式的代码:

import networkx as nx
import csv

def _get_graph_file():
   G = nx.DiGraph()

   #Read the csv file
   file_obj = open('file.csv')

   #Pass the file object to csv reader
   git = csv.reader(file_obj,delimiter=',')

   #Ignore the headers
   headers = git.next()

   #Ignore the line between headers and actual data
   git.next()

   #git is the variable to be passed, not git_df
   G.add_weighted_edges_from(git)

   return G

my_graph = _get_graph_file()

#To get the list of nodes
print my_graph.nodes()

#To get the list of edges
print my_graph.edges()

#To get the weight between two edges
print my_graph['Ken']['heartbeat']['weight']

您需要将文件对象传递给csv读取器,而不是直接传递文件。此外,用于添加具有边权重的节点的语法不正确。您使用的变量名不正确

以下是具有正确语法和格式的代码:

import networkx as nx
import csv

def _get_graph_file():
   G = nx.DiGraph()

   #Read the csv file
   file_obj = open('file.csv')

   #Pass the file object to csv reader
   git = csv.reader(file_obj,delimiter=',')

   #Ignore the headers
   headers = git.next()

   #Ignore the line between headers and actual data
   git.next()

   #git is the variable to be passed, not git_df
   G.add_weighted_edges_from(git)

   return G

my_graph = _get_graph_file()

#To get the list of nodes
print my_graph.nodes()

#To get the list of edges
print my_graph.edges()

#To get the weight between two edges
print my_graph['Ken']['heartbeat']['weight']

Pandas的工作方式与您希望CSV模块工作的方式更为相似,特别是考虑到您可能希望将ccount列转换为数字。它还内置了在开始时跳过该空行的支持

import networkx as nx
import csv
import pandas as pd

def _get_graph_file():
    G= nx.DiGraph()
    git = pd.read_csv('file.csv', skiprows=[1])
    G.add_weighted_edges_from(git.values)
    return G

G = _get_graph_file()
print(G['kts']['blocklist-ipsets'])
输出:


{'weight':2192232}

Pandas的工作方式与您希望CSV模块工作的方式更为相似,特别是考虑到您可能希望将ccount列转换为数字。它还内置了在开始时跳过该空行的支持

import networkx as nx
import csv
import pandas as pd

def _get_graph_file():
    G= nx.DiGraph()
    git = pd.read_csv('file.csv', skiprows=[1])
    G.add_weighted_edges_from(git.values)
    return G

G = _get_graph_file()
print(G['kts']['blocklist-ipsets'])
输出:


{'weight':2192232}

git.next()仅适用于Python2.7,但不适用于3.x,尝试使用next(git),但仍显示错误。但是解释很有帮助。文件“”,第27行打印我的图表。节点()^SyntaxError:invalid syntaxIt显示打印语句中的错误。由于您使用的是python 3,请更改print语句的语法。请注意,此处的权重将是一个字符串,这可能不是您想要的-您需要分析第三列。@MohammedKashif you have greategit.next()仅适用于python 2.7,而不适用于3.x,尝试使用next(git),但仍然显示错误。但是解释很有帮助。文件“”,第27行打印我的图表。节点()^SyntaxError:invalid syntaxIt显示打印语句中的错误。由于您使用的是python 3,请更改print语句的语法。请注意,此处的权重将是一个字符串,这可能不是您想要的-您需要分析第三列。@MohammedKashif you have greated have not print any.@josephnkoro我不确定您希望从print语句中看到什么。我已更改为允许您查看图表构造是否正确。未打印任何内容。@josephnkoro我不确定您希望从打印语句中看到什么。我把它改成了,这样你就可以看到图表的构造是正确的。