Module for reading CSV data

Cliff Wells logiplexsoftware at earthlink.net
Fri Nov 9 21:11:11 CET 2001


On Friday 09 November 2001 11:20, David A McInnis wrote:
> Is there a module for reading CSV files into a dictionary?  Or is there a
> builtin that is used in conjunction with readlines()?
>
> The first row of my csv file contains field names.
>

How do you plan on storing the data in a dictionary?  What would be the 
dictionary key?

Is this what you're looking for?

l = [
    ['a', 'b', 'c', 'd', 'e'], # this is the column names
    [1, 2, 3, 4, 5],   # some data
    [6, 7, 8, 9, 10],
    ]

d = {}

for h in range(len(l[0])):
    d[l[0][h]] = [row[h] for row in l]

The result is a dictionary (d) keyed on the column name ('a', 'b', 'c'...) 
with each item containing a list of data.

Hope this helps.

-- 
Cliff Wells
Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308
(800) 735-0555 x308




More information about the Python-list mailing list