Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 574
NickCHK Avatar asked Dec 10 '13 23:12

NickCHK


People also ask

How do I read a CSV file twice?

You cannot read the same open file twice. Either rewind it before reading for the second time, or close and open again. Better yet, count everything you need when you read the file for the first time.

How does DictReader work in Python?

Python CSV DictReaderDictReader class operates like a regular reader but maps the information read into a dictionary. The keys for the dictionary can be passed in with the fieldnames parameter or inferred from the first row of the CSV file.

What is the difference between reader () and DictReader () function?

Reader() allows you to access CSV data using indexes and is ideal for simple CSV files. csv. DictReader() on the other hand is friendlier and easy to use, especially when working with large CSV files.

What is the use of DictReader () function?

DictReader() class can be used to read a CSV file as a dictionary.


2 Answers

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
like image 61
Pyrce Avatar answered Oct 28 '22 23:10

Pyrce


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.

like image 42
abarnert Avatar answered Oct 29 '22 01:10

abarnert