NickCHK
NickCHK

Reputation: 1233

Python: How do I use DictReader twice?

This feels like a very basic question, but I can't find any mention of it elsewhere. I'm a beginning Python user.

When I read in data using DictReader, and then use the dictionary, I'm unable to reference it again. For example, using this code:

#!/usr/bin/python

import csv
import cgi
import cgitb
cgitb.enable()

print "<head><title>Title</title></head><body>"

f = open("blurbs.csv","rb")
blurbs = csv.DictReader(f, delimiter="\t")
for row in blurbs:
    print row
for row in blurbs:
    print row

f.close()
print "</body>"

Will only print out the contents of blurbs.csv once. The second "for row in blurbs:" does nothing. Is there something I'm missing? How can I make the dictionary into something I can reference repeatedly?

Upvotes: 14

Views: 6341

Answers (3)

Jacob Lee
Jacob Lee

Reputation: 461

If you want to re-use the reader, you could seek the file back to 0. But, if the first row int the csv are headings, then that will be part of the output:

>>> f = open( 'file.csv', 'rbU' )
>>> reader = csv.DictReader( f )
>>> reader.next()
{'col1': '6', 'col2': '0.9', 'col3': '8'}
>>> f.seek(0)
>>> reader.next()
{'col1': 'col1', 'col2': 'col2', 'col3': 'col3'}
>>> f.close()

DictReader uses the first row as the dictionary keys (if they are not otherwise supplied). Creating a new reader object is a lot simpler. You can also copy the data into a data structure like a list and loop over that.

Upvotes: 2

Pyrce
Pyrce

Reputation: 8571

You just need to seek the file back to the start:

with open("blurbs.csv","rb") as f:
    blurbs = csv.DictReader(f, delimiter="\t")
    for row in blurbs:
        print row
    f.seek(0)
    for row in blurbs:
        print row

Alternatively you can wrap the dictionary generation into a list of dicts and operate on that:

with open("blurbs.csv","rb") as f:
    blurbs = list(csv.DictReader(f, delimiter="\t"))
for row in blurbs:
    print row
for row in blurbs:
    print row

Upvotes: 15

abarnert
abarnert

Reputation: 365945

In Python (and almost all computer languages), if you want to store something, you have to do that explicitly. Just printing it out doesn't keep it around anywhere except on the screen.

To use each dictionary repeatedly, but only one at a time, that's easy; row is already storing each dictionary, one at a time:

for row in blurbs:
    print row
    print row
    print row

To use all of the dictionaries repeatedly, you need to store all of them somewhere.

They are already in blurbs, but blurbs is an iterator—something you can loop over once. Once you finish it, there's nothing left in it. That's why your second loop prints nothing.

You want a sequence—something you can index, search, loop over dozens of times, etc. The obvious sequence type to use, when there are no special cases to worry about, is a list. So:

with open("blurbs.csv","rb") as f:
    blurbs = csv.DictReader(f, delimiter="\t")
    rows = list(blurbs)

for row in rows:
    print row
print rows[13]
for row in rows:
    print row
print sorted(rows)

The Tutorial section on Iterators and the following sections explain some of this.

Upvotes: 8

Related Questions