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. Let’s check by printing out each dictionary. We can do this by printing each item in the DictReader object:
Discussions

python - Replace fieldnames when using DictReader - Stack Overflow
I have a test.csv file: foo,bar,foobar,barfoo 1,2,3,4 5,6,7,8 9,10,11,12 And the following CSV parser: #!/usr/bin/env python # -*- coding: utf-8 -*- import csv import json f = open ( 'test.csv... More on stackoverflow.com
🌐 stackoverflow.com
python - Reading column names alone in a csv file - Stack Overflow
Python3 ppl should do: with open(csv_path, "rt") as f: Open in text mode to avoid iterator error (csv.Error: iterator should return strings, not bytes) 2020-04-13T21:27:36.433Z+00:00 ... Save this answer. ... Show activity on this post. The csv.DictReader object exposes an attribute called fieldnames, and that is what you'd use. Here's example ... 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
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
🌐
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 ...
🌐
ProgramCreek
programcreek.com › python › example › 1483 › csv.DictReader
Python Examples of csv.DictReader
def get_msr_paraphrase() -> Dict[str, List[Dict[str, str]]]: url = 'https://raw.githubusercontent.com/wasiahmad/paraphrase_identification/master/dataset/msr-paraphrase-corpus/msr_paraphrase_{}.txt' # NOQA root = download.get_cache_directory(os.path.join('datasets', 'msr_paraphrase')) def creator(path): dataset = {} fieldnames = ('quality', 'id1', 'id2', 'string1', 'string2') for split in ('train', 'test'): data_path = gdown.cached_download(url.format(split)) with io.open(data_path, 'r', encoding='utf-8') as f: f.readline() # skip header reader = csv.DictReader(f, delimiter='\t', fieldnames=fie
🌐
Runebook.dev
runebook.dev › en › docs › python › library › csv › csv.DictReader.fieldnames
Python CSV Headers: The Complete Guide to csv.DictReader.fieldnames
import csv # Assume 'data.csv' exists with headers with open('data.csv', mode='r', newline='', encoding='utf-8') as f: reader = csv.DictReader(f) print(reader.fieldnames) # This might print 'None' if the file hasn't been read yet!
🌐
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 - Now once, you understand how we can use CSV.DictReader for reading the CSV file. 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. If it is left blank, the interpreter itself interpret the first line of the file as the key values. Let’s see an example ...
🌐
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. The columns in each subsequent row then behave like dictionary values and can be accessed with the appropriate key. If the first row of your CSV does not contain your column names, you can pass a fieldnames parameter into the DictReader's constructor to assign the dictionary keys manually.
Find elsewhere
🌐
ZetCode
zetcode.com › python › csv
Python CSV - read, write CSV in Python
January 29, 2024 - #!/usr/bin/python import csv with open('items.csv', 'r') as f: reader = csv.reader(f, delimiter="|") for row in reader: for e in row: print(e) The code example reads and displays data from a CSV file that uses a '|' delimiter. $ ./read_csv2.py pen cup bottle chair book tablet · 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.
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']
🌐
Runebook.dev
runebook.dev › en › docs › python › library › csv › csv.DictReader
Beyond DictReader: A Friendly Guide to CSV Processing in Python
... import csv import io data_no_header = """ Alice,30,NY Bob,25,LA """ csvfile = io.StringIO(data_no_header) # Specify the desired column names FIELD_NAMES = ['Person', 'Years', 'Location'] # Pass the list to fieldnames.
🌐
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 ...
🌐
PyTutorial
pytutorial.com › python-csvdictreader-process-csv-files-with-dictionary-structure
PyTutorial | Python csv.DictReader: Process CSV Files with Dictionary Structure
November 10, 2024 - Let's start with a simple example ... CSV file doesn't have headers or if you want to use different names: fieldnames = ['full_name', 'years', 'dept'] with open('employees.csv', 'r') as file: reader = csv.DictReader(file, ...
🌐
LabEx
labex.io › tutorials › python-how-to-create-dictionaries-from-csv-data-in-python-397975
How to create dictionaries from CSV data in Python | LabEx
import csv ## CSV file with no header row with open('data.csv', 'r') as file: reader = csv.DictReader(file, fieldnames=['Name', 'Age', 'City']) for row in reader: print(row) This will produce the same output as the previous example, but without ...
🌐
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 - import csv with open('example.csv', mode='r', newline='') as file: reader = csv.DictReader(file) # Print the column names print(f"Column names: {', '.join(reader.fieldnames)}") # Process and print each row as an ordered dictionary for row in reader: print(row) ... Column names: Name, Age, Occupation OrderedDict([('Name', 'Alice'), ('Age', '30'), ('Occupation', 'Software Developer')]) OrderedDict([('Name', 'Bob'), ('Age', '25'), ('Occupation', 'Data Scientist')]) OrderedDict([('Name', 'Charlie'), ('Age', '35'), ('Occupation', 'Product Manager')]) The `csv.DictReader` class in Python is a useful tool for reading CSV files, wherein each row is represented as an ordered dictionary with column names as keys.
🌐
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 - import csv fieldnames = ['country_name', 'area', 'code2', 'code3'] with open('country.csv', encoding="utf8") as f: csv_reader = csv.DictReader(f, fieldnames) next(csv_reader) for line in csv_reader: print(f"The area of {line['country_name']} ...
🌐
Arashout
arashout.site › posts › field_by_field_comparison_with_dictreader
Python's csv.DictReader for easy field by field comparisons
For the data with the same primary key in both CSV’s we want to do a field by field comparison. This is where the power of csv.DictReader shines.
🌐
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.
🌐
Gadzmo
gadzmo.com › python › reading and writing csv files with python dictreader and dictwriter
Reading and Writing CSV Files with Python DictReader and DictWriter - Gadzmo
August 16, 2012 - test_array = [] test_array.append({'fruit': 'apple', 'quantity': 5, 'color': 'red'}); test_array.append({'fruit': 'pear', 'quantity': 8, 'color': 'green'}); test_array.append({'fruit': 'banana', 'quantity': 3, 'color': 'yellow'}); test_array.append({'fruit': 'orange', 'quantity': 11, 'color': 'orange'}); fieldnames = ['fruit', 'quantity', 'color'] test_file = open('test2.csv','wb') csvwriter = csv.DictWriter(test_file, delimiter=',', fieldnames=fieldnames) csvwriter.writerow(dict((fn,fn) for fn in fieldnames)) for row in test_array: csvwriter.writerow(row) test_file.close()