Looping through File Question

Thomas Nelson thn at mail.utexas.edu
Wed Sep 5 13:56:29 EDT 2007


> > > On Sep 5, 8:58 pm, planetmatt <planetm... at gmail.com> wrote:
>
> > > > I am a Python beginner.  I am trying to loop through a CSV file which
> > > > I can do.  What I want to change though is for the loop to start at
> > > > row 2 in the file thus excluding column headers.

The DictReader object automatically does this for you:

>>> data = """Name,age,job
... Tom,21,programmer
... Sara,22,chef
... Molly,23,doctor"""
>>> from csv import DictReader
>>> myStuff = DictReader(data.split('\n'))
>>> for line in myStuff:
...     print line
...
{'job': 'programmer', 'age': '21', 'Name': 'Tom'}
{'job': 'chef', 'age': '22', 'Name': 'Sara'}
{'job': 'doctor', 'age': '23', 'Name': 'Molly'}

HTH,
Tom




More information about the Python-list mailing list