networkx读取csv_从CSV文件中提取列以在NetworkX中用作节点列表

I have a CSV file with 2 columns: user and locations. I want to create two lists: one with only users and the other with only locations so that I can use the draw_network_nodes(nodelist=...) function in networkx to draw users and locations separately as nodes with different shapes and colors (all users will be blue boxes and all locations will be red circles).

Also, there is a header in my CSV file, so I do not want the header names to be part of either lists.

解决方案

Building on top of Hai Vu's answer:

import csv

def reader(filename):

for (lineno, line) in enumerate(open(filename)):

if lineno > 0: # skip header

yield line

filename = "locations.csv"

(users, locations) = zip(*( row for row in csv.reader(reader(filename))))

print "users =", users

print "locations =", locations

Gives:

locations = ('seattle', 'los angeles', 'new york', 'london')

users = ('john', 'alan', 'trish', 'jack')

From:

user,location

john,seattle

alan,los angeles

trish,new york

jack,london

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值