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?
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.
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.
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.
DictReader() class can be used to read a CSV file as a dictionary.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With