Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16006

Re: How to: Coordinate DictReader and Reader for CSV

From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Re: How to: Coordinate DictReader and Reader for CSV
Date 2011-11-21 13:59 +0000
Organization Norwich University
Message-ID <9iv3q7F1t5U1@mid.individual.net> (permalink)
References <2012e1cb-5913-4f39-b102-038a1c95a483@gi1g2000vbb.googlegroups.com>

Show all headers | View raw


On 2011-11-21, ray <ray@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to:  Coordinate DictReader and Reader for CSV ray <ray@aarden.us> - 2011-11-21 05:18 -0800
  Re: How to:  Coordinate DictReader and Reader for CSV Neil Cerutti <neilc@norwich.edu> - 2011-11-21 13:59 +0000
    Re: How to: Coordinate DictReader and Reader for CSV ray <ray@aarden.us> - 2011-11-21 07:16 -0800
      Re: How to: Coordinate DictReader and Reader for CSV Neil Cerutti <neilc@norwich.edu> - 2011-11-21 15:41 +0000
        Re: How to: Coordinate DictReader and Reader for CSV Neil Cerutti <neilc@norwich.edu> - 2011-11-21 15:43 +0000
      Re: How to: Coordinate DictReader and Reader for CSV Tim Chase <python.list@tim.thechases.com> - 2011-11-21 12:36 -0600

csiph-web