graphbuilder.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:graph_tools_networkx 作者: AirtrafficAnalysisGroup 项目源码 文件源码
def build_graph_for_file(file_path, dir_name, name):
    data = open(file_path, 'r')
    G=nx.DiGraph()
    rows = csv.reader(data, quoting=csv.QUOTE_NONNUMERIC)
    next(rows) #skip the header
    for row in rows:
        row_fil = list(filter(lambda x: type(x) is float, row))
        if G.has_node(row_fil[0]) is not True:
            G.add_node(row_fil[0], market_id=row_fil[1])
        if G.has_node(row_fil[2]) is not True:
            G.add_node(row_fil[2], market_id=row_fil[3])
        if G.has_edge(row_fil[0], row_fil[2]):
            old = G.get_edge_data(row_fil[0], row_fil[2])
            G.add_edge(row_fil[0], row_fil[2], num_of_people=old['num_of_people'] + row_fil[4], total_price=old['total_price'] + row_fil[5])
        else:
            G.add_edge(row_fil[0], row_fil[2], num_of_people=row_fil[4], total_price=row_fil[5])

    output_file_path = ('graphs/' + name + '.gexf') 
    nx.write_gexf(G, output_file_path)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号