How to Get Column Names from CSV File in Python?

By Hardik Savani October 30, 2023 Category : Python

Hello Artisan,

This tutorial shows you python get column names from csv file. We will look at an example of how to get header from csv file in python. This article will give you a simple example of python csv get header names. If you have a question about how to get column names from csv file in python then I will give a simple example with a solution.

In this example, we will take one demo.csv file with ID, Name and Email fields. Then, we will use open(), next() and reader() functions to read header from csv file.

I will give you one example for reading csv file column names in python, so Without any further ado, let's see below code example:

You can use these examples with python3 (Python 3) version.

Example 1:

main.py

from csv import reader
  
# skip first line from demo.csv
with open('demo.csv', 'r') as readObj:
  
    csvReader = reader(readObj)
    # Get CSV File Columns Name
    header = next(csvReader)
  
    print(header)

Output:

['ID', 'Name', 'Email']

I hope it can help you...

Tags :
Shares