Python's CSV reader

Peter Otten __peter__ at web.de
Thu Aug 4 11:42:22 EDT 2005


Stephan wrote:

> Thank you all for these interesting examples and methods!

You're welcome.

> Supposing I want to use DictReader to bring in the CSV lines and tie
> them to field names, (again, with alternating lines having different
> fields), should I use two two DictReaders as in Christopher's example
> or is there a better way?

For a clean design you would need not just two DictReader instances, but one
DictReader for every two lines. 
However, with the current DictReader implementation, the following works,
too:

import csv
import sys

reader = csv.DictReader(sys.stdin)

for record in reader:
    print record
    reader.fieldnames = None

Peter




More information about the Python-list mailing list