From the documentation of csv, the first argument to csv.reader or csv.DictReader is csvfile -

csvfile can be any object which supports the iterator protocol and returns a string each time its __next__() method is called — file objects and list objects are both suitable.

In your case when you give the string as the direct input for the csv.DictReader() , the __next__() call on that string only provides a single character, and hence that becomes the header, and then __next__() is continuously called to get each row.

Hence, you need to either provide an in-memory stream of strings using io.StringIO:

>>> import csv
>>> s = """a,b,c
... 1,2,3
... 4,5,6
... 7,8,9"""
>>> import io
>>> reader_list = csv.DictReader(io.StringIO(s))
>>> print(reader_list.fieldnames)
['a', 'b', 'c']
>>> for row in reader_list:
...     print(row)
... 
{'a': '1', 'b': '2', 'c': '3'}
{'a': '4', 'b': '5', 'c': '6'}
{'a': '7', 'b': '8', 'c': '9'}

or a list of lines using str.splitlines:

>>> reader_list = csv.DictReader(s.splitlines())
>>> print(reader_list.fieldnames)
['a', 'b', 'c']
>>> for row in reader_list:
...     print(row)
... 
{'a': '1', 'b': '2', 'c': '3'}
{'a': '4', 'b': '5', 'c': '6'}
{'a': '7', 'b': '8', 'c': '9'}
Answer from Anand S Kumar on Stack Overflow
🌐
Python
docs.python.org › 3 › library › csv.html
csv — CSV File Reading and Writing
Programmers can also read and write data in dictionary form using the DictReader and DictWriter classes. ... The Python Enhancement Proposal which proposed this addition to Python. ... Return a reader object that will process lines from the given csvfile. A csvfile must be an iterable of strings, each in the reader’s defined csv format.
Discussions

python - understanding csv DictReader, what it returns? - Stack Overflow
no mind about result of csv.DictReader, so and no possibility to know, are csv params specified correctly. i have input Device tps kB_read/s kB_wrtn/s kB_dscd/s kB_read k... More on stackoverflow.com
🌐 stackoverflow.com
When using reader or DictReader from the csv module, why do you need to access the return value while the file is still open?
because leaving the with-block calls the object's __exit__() function https://peps.python.org/pep-0343/ which for a filehandle is equivalent to a close() https://docs.python.org/3/reference/datamodel.html#object.__exit__ so after you jump out of the with-block's indent, the file handle's close function has been called and it's no longer readable More on reddit.com
🌐 r/learnpython
11
3
August 7, 2023
What does .dictreader() do?
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, ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
0
September 27, 2023
csv.DictReader marked as producing Dict[str, str] but can have a None key
import csv from io import StringIO ... = csv.DictReader(StringIO(csvtext), delimiter=',') #def checkrow(row: dict[str | None, str]) -> None: def checkrow(row: dict[str, str]) -> None: if None in row: print(f"None found as key with value {row[None]!r}, someone broke a promise!") for row in csvreader: print(row) checkrow(row) Type checkers complain: error: Invalid index type "None" for "Dict[str, str]"; expected type "str" Signature visible here: https://github.com/python/typeshed/... More on github.com
🌐 github.com
6
May 25, 2022
🌐
University of Washington
courses.cs.washington.edu › courses › cse160 › 22au › computing › csv-parsing.html
How to parse csv formatted files using csv.DictReader
people_csv = open("people.csv") input_file = csv.DictReader(people_csv) You may then iterate over the rows of the csv file by iterating over input_file. (Similarly to other files, you need to re-open the file if you want to iterate a second time.) ... Remember how when you iterate over a normal file, each iteration of the loop produces a single string that represents the contents of that line.
🌐
Medium
medium.com › @3valuedlogic › using-python-csv-3-dictreader-e4814ce2e44
Using Python CSV #3 — DictReader
December 22, 2022 - The csv.DictReader is a function returns a DictReader object given a csv file. If we iterate over it, we see it contains dictionaries whose elements (keys and values) are strings.
🌐
Brodan
brodan.biz › blog › parsing-csv-files-with-python
Parsing CSV Files with Python's DictReader - Brodan.biz
August 24, 2018 - The DictReader class basically creates a CSV object that behaves like a Python OrderedDict. It works by reading in the first line of the CSV and using each comma separated value in this line as a dictionary key.
🌐
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, CSV.Dictreader creates a dictionary object for each row of the CSV file and maps each row value to the column name as the key. Keys and Values are stored as the string.
Find elsewhere
🌐
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!

🌐
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...
🌐
GitHub
github.com › python › typeshed › issues › 7953
csv.DictReader marked as producing Dict[str, str] but can have a None key · Issue #7953 · python/typeshed
May 25, 2022 - Here is an example to illustrate: import csv from io import StringIO csvtext = "foo,bar\n1,2\n3,4,5" csvreader = csv.DictReader(StringIO(csvtext), delimiter=',') #def checkrow(row: dict[str | None, str]) -> None: def checkrow(row: dict[s...
Author   python
🌐
GitHub
gist.github.com › 89465127 › 5273390
Uses csv.DictReader and StringIO to read a sample csv file. No external file is required. · GitHub
Uses csv.DictReader and StringIO to read a sample csv file. No external file is required. - csv_dictreader_example.py
🌐
ZetCode
zetcode.com › python › csv
Python CSV - read, write CSV in Python
January 29, 2024 - #!/usr/bin/python import csv with ... displays data from a CSV file that uses a '|' delimiter. ... The csv.DictReader class operates like a regular reader but maps the information read into a dictionary....
🌐
GeeksforGeeks
geeksforgeeks.org › load-csv-data-into-list-and-dictionary-using-python
Load CSV data into List and Dictionary using Python - GeeksforGeeks
April 28, 2025 - from csv import DictReader # open file in read mode with open("geeks.csv", 'r') as f: dict_reader = DictReader(f) list_of_dict = list(dict_reader) print(list_of_dict) ... In Python, lists can contain multiple dictionaries, each holding key-value ...
🌐
Compciv
2017.compciv.org › guide › topics › python-standard-library › csv.html
csv - reading and writing delimited text data
Use the csv.DictReader class. However, initializing the DictReader object requires passing in a fieldnames argument, a list of the exact column headers you intend to include, as well as their exact order. DictReader can’t infer from individual dict objects alone what the CSV structure should be like:
🌐
TutorialKart
tutorialkart.com › python › how-to-read-csv-files-using-csv-dictreader-in-python
How to Read CSV Files using csv.DictReader in Python
February 17, 2025 - We create a csv.DictReader object. We iterate over each row and access specific columns using dictionary keys row['Name'] and row['Grade']. We print the extracted values using an f-string.
🌐
PyTutorial
pytutorial.com › python-csvdictreader-process-csv-files-with-dictionary-structure
PyTutorial | Python csv.DictReader: Process CSV Files with Dictionary Structure
November 10, 2024 - with open('incomplete.csv', 'r') as file: reader = csv.DictReader(file) for row in reader: age = row['age'] if row['age'] else 'Not specified' print(f"Name: {row['name']}, Age: {age}") DictReader reads all values as strings.
🌐
GitHub
gist.github.com › casperghst42 › 7e748e5f2373aee5ca4543df865fa64d
Python: reading CSV into a dict · GitHub
Python: reading CSV into a dict · Raw · gistfile1.txt · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.