The csv.DictReader function expects a file object as its first argument, not a string. So, it always is used with normal file io.

for example,

with open('input.csv', 'r') as f:
    reader = csv.DictReader(f, delimiter=' ', fieldnames = ['Device', 'tps', 'kB_read/s' ,'kB_wrtn/s', 'kB_dscd/s', 'kB_read', 'kB_wrtn', 'kB_dscd'])

And, you can iterate the reader (i.e., iterable of dicts) row by row.

for row in reader:
    print(row)

The row variable contains dictionary-like object that you can easily access by key, for example, row['Device'].

Answer from JayPeerachai on Stack Overflow
🌐
University of Washington
courses.cs.washington.edu › courses › cse140 › 13wi › csv-parsing.html
How to parse csv formatted files using csv.DictReader?
This example finds the oldest person ... == None or max_age < age: max_age = age oldest_person = row["name"] if max_age != None: print "The oldest person is %s, who is %d years old."...
🌐
Brodan
brodan.biz › blog › parsing-csv-files-with-python
Parsing CSV Files with Python's DictReader - Brodan.biz
August 24, 2018 - import csv with open('videos.csv') as csvfile: reader = csv.DictReader(csvfile) for row in reader: print(row['id'])
🌐
Medium
medium.com › @3valuedlogic › using-python-csv-3-dictreader-e4814ce2e44
Using Python CSV #3 — DictReader
December 22, 2022 - Our fieldnames should be the first row (the header): ‘name’, ‘age’, and ‘account’. These should map to the name, age, and account size of each person. Let’s check by printing out each dictionary. We can do this by printing each item in the DictReader object:
🌐
ProgramCreek
programcreek.com › python › example › 1483 › csv.DictReader
Python Examples of csv.DictReader
def import_business_data_relations(self, data_source): """ Imports business data relations """ if isinstance(data_source, str): data_source = [data_source] for path in data_source: if os.path.isabs(path): if os.path.isfile(os.path.join(path)): relations = csv.DictReader(open(path, "r")) RelationImporter().import_relations(relations) else: utils.print_message("No file found at indicated location: {0}".format(path)) sys.exit() else: utils.print_message( "ERROR: The specified file path appears to be relative.
🌐
Python Pool
pythonpool.com › home › blog › csv dicteader doesn’t have to be hard
CSV Dicteader Doesn't Have To Be Hard - Python Pool
December 18, 2021 - So, in the above example, what we have done is we first imported DictReader class from the CSV module. After that, we opened the file airtarvel.csv in “read” mode, which consists of the number of monthly travelers in 1958, 1959, and 1960.
🌐
Reddit
reddit.com › r/learnpython › when using reader or dictreader from the csv module, why do you need to access the return value while the file is still open?
r/learnpython on Reddit: When using reader or DictReader from the csv module, why do you need to access the return value while the file is still open?
August 7, 2023 -

Ok, this will make more sense. Why does this work:

import csv

with open('test.csv') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        print(row)

But this gives an error that the file is closed:

import csv

with open('test.csv') as csv_file:
    csv_reader = csv.DictReader(csv_file)

for row in csv_reader:
    print(row)

Traceback (most recent call last):
File "c:\Users\John\Documents\Python\test_project\test2.py", line 5, in <module> for row in csv_reader: File "C:\Users\John\AppData\Local\Programs\Python\Python311\Lib\csv.py", line 110, in next self.fieldnames File "C:\Users\John\AppData\Local\Programs\Python\Python311\Lib\csv.py", line 97, in fieldnames self._fieldnames = next(self.reader) ^ 
ValueError: I/O operation on closed file.

Does DictReader (and also reader) not just run once and return a value? Why is csv_file being accessed after the line it's used in?

Thanks!

Find elsewhere
🌐
Programiz
programiz.com › python-programming › reading-csv-files
Reading CSV files in Python
The csv.DictReader() returned an OrderedDict type for each row. That's why we used dict() to convert each row to a dictionary. Notice that we have explicitly used the dict() method to create dictionaries inside the for loop. ... Note: Starting from Python 3.8, csv.DictReader() returns a dictionary for each row, and we do not need to use dict() explicitly.
🌐
Linux Hint
linuxhint.com › use-python-csv-dictreader
Linux Hint – Linux Hint
December 2, 2021 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Python
docs.python.org › 3 › library › csv.html
csv — CSV File Reading and Writing
Return the next row of the reader’s iterable object as a list (if the object was returned from reader()) or a dict (if it is a DictReader instance), parsed according to the current Dialect.
🌐
YouTube
youtube.com › kris jordan
Read CSV Files in Python with csv.DictReader - Iterate through Rows as Dicts with Columns as Keys - YouTube
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published   October 6, 2020
Views   2K
🌐
DEV Community
dev.to › bowmanjd › flexible-csv-handling-in-python-with-dictreader-and-dictwriter-3hae
Flexible CSV Handling in Python with DictReader and DictWriter - DEV Community
February 9, 2026 - inpath = Path("sample.csv") outpath = Path("out/transformed.csv") args = {"newline": "", "encoding": "utf-8-sig"} with inpath.open("r", **args) as infile, outpath.open("w", **args) as outfile: reader = csv.DictReader(infile) writer = csv.DictWriter(outfile, ["Firstname", "Lastname", "Username"]) As you may already be aware, it is possible to pass a dictionary of keyword arguments to a Python function. Because both open() functions needed the same newline and encoding arguments, I built a dictionary with those then pass it to each open(), prefixing the necessary **. That way I don't repeat myself. The above (incomplete) example can be filled out with row handling logic so that each row can be read, transformed, then written.
🌐
Stack Overflow
stackoverflow.com › questions › 72955906 › python-csv-dictreader
Python csv.DictReader - Stack Overflow
I was trying different codes with csv.DictReader to read csv file into a variable. #option 1 dna = [] with open(sys.argv[1], "r") as file: dna = csv.DictReder(file) print(dna) #output...
🌐
Codecademy Forums
discuss.codecademy.com › data science
What does .dictreader() do? - Data Science - Codecademy Forums
September 27, 2023 - hi guys! I have a question. I know when we use .dictreader() we use csv module to make a dictionary out of the csv file, so it’s easier to use for programming, but I don’t get it how does this dictionary look like exactly. first, I thought it uses titles in the first line to make keys, and then unzip the rest into these keys based on their index. but now, I see it like this, which doesn’t make sense. import csv with open("cool_csv.csv") as cool_csv_file : cool_csv_dict = csv.DictReader(coo...
🌐
GITNUX
blog.gitnux.com › home › programming-advice
How do I use the csv.DictReader class in Python to read a CSV file? • GITNUX
August 25, 2023 - 3. Create a `DictReader` object, passing the file object as an argument: ... Here’s a complete example that reads a CSV file named `example.csv` and prints each row as an ordered dictionary:
🌐
Python Beginners
python-adv-web-apps.readthedocs.io › en › latest › dicts.html
Dictionaries — Python Beginners documentation
You can use Python’s csv module and the DictReader() method to create a list of dictionaries from a CSV file (also see dictreader_example2.py here).
🌐
Real Python
realpython.com › videos › reading-csvs-pythons-csv-module
Reading CSVs With Python's "csv" Module (Video) – Real Python
I think you should try downloading the file or using: docs.python.org/3/library/urllib.request.html#urllib.request.urlopen · Kim on Oct. 31, 2021 ... I just went through this tutorial and with DictReader, it appears to have iterated rows through all of the rows, but only printed rows 0 and 2 and bypassed 1. Here is the snippet, which I believe is identical to the script in the tutorial. with open(r'C:\Users\xxxx\xxxx\csv_example.txt') as csv_file: csv_reader = csv.DictReader(csv_file, delimiter=',') line_count = 0 for row in csv_reader: if line_count ==0: print(f'Column names are {", ".join(row)}') line_count += 1 else: print(f'\t{row["name"]} works in the {row["department"]} department, and was born in {row["birthday month"]}.') line_count += 1 print(f'Processed {line_count} lines.')
Published   March 1, 2019
🌐
CodeRivers
coderivers.org › blog › dictreader-python
Mastering `DictReader` in Python: A Comprehensive Guide - CodeRivers
March 22, 2025 - It then reads the first row of the file as the header row. Each subsequent row is read and mapped to a dictionary, where the keys are the values from the header row, and the values are the corresponding values in that row. For example, consider a simple CSV file named data.csv with the following ...