Skip to content Skip to sidebar Skip to footer

Extract Column From CSV File To Use As Nodelist In 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(nod

Solution 1:

Since you provided no input, expected output, I am making some assumption about them. Assume that the input file is called data.csv:

user,location
john,seattle
alan,los angeles
trish,new york

The script to split the csv into two files is called csv_split.py:

import csv

with open('data.csv') as csv_in,          \
     open('users.txt', 'w') as users_out, \
     open('locations.txt', 'w') as locations_out:
    csv_dict_reader = csv.DictReader(csv_in)
    for line in csv_dict_reader:
        users_out.write(line['user'] + '\n')
        locations_out.write(line['location'] + '\n')

Discussion

  • My code is for demonstration purpose, hence does not provide any error checking.
  • The csv.DictReader() class assumes the first line to be header and use that as the keys for each line

Solution 2:

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

Post a Comment for "Extract Column From CSV File To Use As Nodelist In NetworkX"