The easiest way is to just set:

reader.fieldnames = "email", "name", "id",  "phone"

You can save the old fieldnames if you want too.

Answer from jamylak on Stack Overflow
🌐
Python
docs.python.org › 3 › library › csv.html
csv — CSV File Reading and Writing
If the argument passed to fieldnames is an iterator, it will be coerced to a list. Changed in version 3.6: Returned rows are now of type OrderedDict. Changed in version 3.8: Returned rows are now of type dict. ... >>> import csv >>> with open('names.csv', newline='') as csvfile: ... reader = csv.DictReader(csvfile) ...
🌐
Medium
medium.com › @3valuedlogic › using-python-csv-3-dictreader-e4814ce2e44
Using Python CSV #3 — DictReader
December 22, 2022 - Fieldnames are either explicitly specified or are the first row of the csv. 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.
Discussions

python - Replace fieldnames when using DictReader - Stack Overflow
So the fieldnames don't have to correspond with the actual fieldnames of the header line in the CSV? 2013-06-11T08:42:15.86Z+00:00 ... @cherrun No they don't. On a side note, You don't even need to specify them like you did. csv.DictReader(f) will read the first line as the header by default, ... 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
python - Reading column names alone in a csv file - Stack Overflow
I'm using python 3.7. I think there was a change in what the DictReader returns in python3.6. 2019-07-07T23:20:26.38Z+00:00 ... Interesting - for what it's worth, a hello world CSV reader in Python 3.7 gives proper fieldnames for me 2019-09-12T18:29:52.497Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
python - Read CSV items with column name - Stack Overflow
... If the fieldnames parameter is omitted, the values in the first row of the csvfile will be used as the fieldnames. ... Sign up to request clarification or add additional context in comments. ... You can use a csv.DictReader instance to get this behaviour. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Vertabelo Academy
academy.vertabelo.com › course › python-csv › reading › reading › importing-csv-contents-using-custom-field-names
Read and Write CSV in Python | Learn Python | Vertabelo Academy
import csv with open('departments.csv') as csv_file: csv_reader = csv.DictReader(csv_file, fieldnames=['Department Name', 'Number of Employees', 'Department Budget']) line_count = 0 for row in csv_reader: if line_count > 0 and float(row['Department ...
🌐
Runebook.dev
runebook.dev › en › docs › python › library › csv › csv.DictReader.fieldnames
Python CSV Headers: The Complete Guide to csv.DictReader.fieldnames
When you use csv.DictReader, it ... field names (column headers) and the values are the row's data. The .fieldnames attribute simply holds a list of these header strings, in the order they appear in the CSV file....
🌐
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!

🌐
Brodan
brodan.biz › blog › parsing-csv-files-with-python
Parsing CSV Files with Python's DictReader - Brodan.biz
August 24, 2018 - The columns in each subsequent ... does not contain your column names, you can pass a fieldnames parameter into the DictReader's constructor to assign the dictionary keys manually....
🌐
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 - Let’s understand the use of different parameters available for initializing the object. f: It is the variable name of the csv file that we need to read. fieldnames: This argument is used to set the key values for the dictionary object.
Find elsewhere
Top answer
1 of 10
155

Though you already have an accepted answer, I figured I'd add this for anyone else interested in a different solution-

  • The csv module's DictReader object has a public attribute called fieldnames (as of Python 2.6 and above). https://docs.python.org/3.4/library/csv.html#csv.csvreader.fieldnames

An implementation could be as follows:

import csv

with open('C:/mypath/to/csvfile.csv', 'r') as f:
    dict_reader = csv.DictReader(f)

    #get header fieldnames from DictReader and store in list
    headers = dict_reader.fieldnames

    #sample file reading logic
    for line in dict_reader:
        print(line[headers[0]])
        

In the above, dict_reader.fieldnames returns a list of your headers (assuming the headers are in the top row). Which allows...

>>> print(headers)
['MyColumn1', 'MyColumn2', 'MyColumn3']

If your headers are in, say the 2nd row (with the very top row being row 1), you could do as follows:

import csv

with open('C:/mypath/to/csvfile.csv', 'r') as f:
    #you can eat the first line before creating DictReader.
    #if no "fieldnames" param is passed into
    #DictReader object upon creation, DictReader
    #will read the upper-most line as the headers
    f.readline()
    
    dict_reader = csv.DictReader(f)
    headers = dict_reader.fieldnames

    #sample file reading logic
    for line in dict_reader:
        print(line[headers[0]])
2 of 10
85

You can read the header by using the next() function which return the next row of the reader’s iterable object as a list. then you can add the content of the file to a list.

import csv
with open("C:/path/to/.filecsv", "rb") as f:
    reader = csv.reader(f)
    i = reader.next()
    rest = list(reader)

Now i has the column's names as a list.

print i
>>>['id', 'name', 'age', 'sex']

Also note that reader.next() does not work in python 3. Instead use the the inbuilt next() to get the first line of the csv immediately after reading like so:

import csv
with open("C:/path/to/.filecsv", "rb") as f:
    reader = csv.reader(f)
    i = next(reader)

    print(i)
    >>>['id', 'name', 'age', 'sex']
🌐
CodeRivers
coderivers.org › blog › python-csv-dictreader
Python `csv.DictReader`: A Comprehensive Guide - CodeRivers
February 22, 2026 - If your CSV file does not have a header row, you can specify the column names yourself. import csv with open('no_header.csv', mode='r', encoding='utf-8') as csv_file: fieldnames = ['col1', 'col2', 'col3'] csv_reader = csv.DictReader(csv_file, fieldnames=fieldnames) next(csv_reader) # Skip the ...
🌐
ZetCode
zetcode.com › python › csv
Python CSV - read, write CSV in Python
January 29, 2024 - #!/usr/bin/python import csv with ... 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....
🌐
GeeksforGeeks
geeksforgeeks.org › get-column-names-from-csv-using-python
Get column names from CSV using Python - GeeksforGeeks
April 7, 2025 - Using Python's CSV library to read the CSV file line and line and printing the header as the names of the columns. Reading the CSV file as a dictionary using DictReader and then printing out the keys of the dictionary.
🌐
GitHub
github.com › python › typeshed › issues › 3713
csv.DictReader.fieldnames should be Optional[Sequence[str]] · Issue #3713 · python/typeshed
February 4, 2020 - csv.DictReader.fieldnames defaults to None, and gets set by reading the first line of data. However if there is no first line of data it remains None. Since fieldnames is not marked as Optional cod...
Author   python
🌐
MicroPyramid
micropyramid.com › home › blog › python › case-insensitive csv.dictreader in python
Case-Insensitive csv.DictReader in Python | MicroPyramid
July 30, 2018 - Blog / Python · July 30, 2018 · Updated June 10, 2026 · 6 min read · To read a CSV with csv.DictReader while ignoring header case and stray whitespace, subclass DictReader and override its fieldnames property so each name is returned ...
🌐
Python Tutorial
pythontutorial.net › home › python basics › python read csv file
How to Read a CSV File in Python Using csv Module
March 30, 2025 - Code language: Python (python) ... of field names to the DictReader() constructor like this: import csv fieldnames = ['country_name', 'area', 'code2', 'code3'] with open('country.csv', encoding="utf8") as f: csv_reader = ...
🌐
Reddit
reddit.com › r/learnpython › csv.dictreader when one of my fields is a list
r/learnpython on Reddit: csv.DictReader when one of my fields is a list
August 29, 2023 -

I would like to read a csv into a dict but one of my fields is a list. Like so:

item, num_ordered, people

apple, 4, ['Janet', 'Sam']

grape, 1, ['Stu', 'Jenny']

I used csv.DictReader without the `fieldnames` kwarg so the header row became the names of the keys. But it's picking up the commas in the lists as the end of a field. How do I get it to recognize that these are lists?

🌐
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...