How to: Coordinate DictReader and Reader for CSV

Neil Cerutti neilc at norwich.edu
Mon Nov 21 08:59:35 EST 2011


On 2011-11-21, ray <ray at aarden.us> wrote:
> I am trying to get the data from a CSV file into variables.  I have
> used DictReader to get the field names and I can report them.  When I
> attempt to look at the data, every row shows the combination of
> fieldname:data.  How do I get the data out?
> linelist=open( "C:/Users/rjoseph/Documents/Projects/Bootstrap Plan
> Design Tool/Sandbox/line_list_r0a.csv", "rb" )
> csvDictReader=csv.DictReader( linelist, dialect='excel' )
> for data in csvReader:
>         print data[0]
>         print data[1]
>         print data[2]
>         print data[3]
> linelist.close()

The elements yielded by a DictReader iterator are dictionaries,
and the keys are the headings of the csv file. So replace those
integers with strings representing the headings of your file.

If the headings are actually those numbers, you want:

[...]
    print data['0']
    print data['1']
    print data['2']
    print data['3']
    print data['4']
[...]

-- 
Neil Cerutti
  "This room is an illusion and is a trap devisut by Satan.  Go
ahead and dauntlessly!  Make rapid progres!"
  --Ghosts 'n Goblins



More information about the Python-list mailing list