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