-1

Super noob here, so this might be a little embarrasing. I need to work with a csv file, and found that you can use csv.DictReader to make a list of ordered dicts. So far so good. I can loop through the list and do stuff, but only one time. If I want to print the dicts 2 times, it doesn´t work.

import csv
csv_file = open('untitled2.csv', mode='r')
csv_reader = csv.DictReader(csv_file, delimiter = ";")

    for rows in csv_reader:
        print (rows)

    for rows in csv_reader:
        print (rows)

This only prints the list of dicts 1 time. I need to go through the list a number of times. but I´m not able to do that.

1 Answers1

0

You need to go to the begining of the file again : csv_file.seek(0) after the first for.

Don't forget to close it when you're done. The best way to do it is in a context :

with open('untitiled2.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file, delimiter=';')
    # your for
Bestasttung
  • 2,388
  • 4
  • 22
  • 34