Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop over rows of csv.DictReader more than once

Tags:

python

csv

I open a file and read it with csv.DictReader. I iterate over it twice, but the second time nothing is printed. Why is this, and how can I make it work?

with open('MySpreadsheet.csv', 'rU') as wb:
    reader = csv.DictReader(wb, dialect=csv.excel)
    for row in reader:
        print row

    for row in reader:
        print 'XXXXX'

# XXXXX is not printed
like image 366
Pyderman Avatar asked Jul 09 '15 16:07

Pyderman


People also ask

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.

What does next () in CSV do?

The csv. reader object is an iterator. An iterator is an object with a next() method that will return the next value available or raise StopIteration if no value is available.

How does CSV DictReader work in Python?

The csv. DictReader 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. The first line of the file consists of dictionary keys.


2 Answers

You read the entire file the first time you iterated, so there is nothing left to read the second time. Since you don't appear to be using the csv data the second time, it would be simpler to count the number of rows and just iterate over that range the second time.

import csv
from itertools import count

with open('MySpreadsheet.csv', 'rU') as f:
    reader = csv.DictReader(f, dialect=csv.excel)
    row_count = count(1)

    for row in reader:
        next(count)
        print(row)

for i in range(row_count):
    print('Stack Overflow')

If you need to iterate over the raw csv data again, it's simple to open the file again. Most likely, you should be iterating over some data you stored the first time, rather than reading the file again.

with open('MySpreadsheet.csv', 'rU') as f:
    reader = csv.DictReader(f, dialect=csv.excel)

    for row in reader:
        print(row)

with open('MySpreadsheet.csv', 'rU') as f:
    reader = csv.DictReader(f, dialect=csv.excel)

    for row in reader:
        print('Stack Overflow')

If you don't want to open the file again, you can seek to the beginning, skip the header, and iterate again.

with open('MySpreadsheet.csv', 'rU') as f:
    reader = csv.DictReader(f, dialect=csv.excel)

    for row in reader:
        print(row)

    f.seek(0)
    next(reader)

    for row in reader:
        print('Stack Overflow')
like image 69
davidism Avatar answered Nov 06 '22 00:11

davidism


add a wb.seek(0) (goes back to the start of the file) and next(reader) (skips the header row) before your second loop.

like image 24
Christopher Pearson Avatar answered Nov 06 '22 00:11

Christopher Pearson