goldb.org home

AS OF MAY 2008, THIS BLOG IS NO LONGER BEING UPDATED.
Visit the new blog at: http://coreygoldberg.blogspot.com



 Wednesday, April 16, 2008

Python - Slurping CSV Files Into Nested Lists

When working with data sets, a common task I need to do is slurp a csv file into a nested data structure that contains a sequence of lists correlating to the rows and values in the csv file.

For example...

File contents (foo.csv):

10,20,30,40
19,29,39,55
16,21,31,59

Result:

[['10', '20', '30', '40'],
['19', '29', '39', '55'],
['16', '21', '31', '59']]

To accomplish this. you could parse it inside a big honkin' list comprehension and build our structure in one step:

csv_file = 'foo.csv'
value_lists = [line.split(',') for line in
[line.strip() for line in open(csv_file, 'r').readlines()]]

You could also use the csv module from Python's standard library:

import csv
csv_file = 'foo.csv'
value_lists = list(csv.reader(open(csv_file, 'r')))

The csv module has some useful tools for reading/writing csv files.  Check it out.

#    Comments [1] |
Wednesday, April 16, 2008 1:08:48 PM (Eastern Standard Time, UTC-05:00)
The csv module is definitely your friend for this, because real-world CSVs have a nasty habit of containing quoted values that can span multiple lines. The big honkin' list comprehension would fall down pretty painfully if it were to encounter such a thing.
Comments are closed.