ItSolutionStuff.com

How to Read a CSV File in Python?

By Hardik Savani • October 30, 2023
Python

Hey Dev,

This tutorial shows you python read csv file example. We will look at an example of python read csv examples. you can understand a concept of how to read csv file in python explain with example. It's a simple example of python get data from csv file.

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

I will give you multiple examples for reading csv file 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: Python Read CSV File

main.py

from csv import reader
  
# open demo.csv file in read mode
with open('demo.csv', 'r') as readObj:
  
    # pass the file object to reader() to get the reader object
    csvReader = reader(readObj)
  
    # Iterate over each row in the csv using reader object
    for row in csvReader:
        # row variable is a list that represents a row in csv
        print(row)

Output:

['ID', 'Name', 'Email']
['1', 'Hardik Savani', 'hardik@gmail.com']
['2', 'Vimal Kashiyani', 'vimal@gmail.com']
['3', 'Harshad Pathak', 'harshad@gmail.com']

Example 2: Python Read CSV File without Header

main.py

from csv import reader
    
# skip first line from demo.csv
with open('demo.csv', 'r') as readObj:
  
    csvReader = reader(readObj)
    header = next(csvReader)
  
    # Check file as empty
    if header != None:
        # Iterate over each row after the header in the csv
        for row in csvReader:
            # row variable is a list that represents a row in csv
            print(row)

Output:

['1', 'Hardik Savani', 'hardik@gmail.com']
['2', 'Vimal Kashiyani', 'vimal@gmail.com']
['3', 'Harshad Pathak', 'harshad@gmail.com']

Example 3: Python Read CSV File Line By Line

main.py

from csv import DictReader
  
# open demo.csv file in read mode
with open('demo.csv', 'r') as readObj:
  
    # Pass the file object to DictReader() to get the DictReader object
    csvDictReader = DictReader(readObj)
  
    # get over each line as a ordered dictionary
    for row in csvDictReader:
        # row variable is a dictionary that represents a row in csv
        print(row)

Output:

{'ID': '1', 'Name': 'Hardik Savani', 'Email': 'hardik@gmail.com'}
{'ID': '2', 'Name': 'Vimal Kashiyani', 'Email': 'vimal@gmail.com'}
{'ID': '3', 'Name': 'Harshad Pathak', 'Email': 'harshad@gmail.com'}

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Add Header in CSV File using Python?

Read Now →

How to Write Multiple Rows in CSV using Python?

Read Now →

Python Write CSV File from List Example

Read Now →

How to Remove Last Element from List in Python?

Read Now →

Python Delete Files Matching Pattern Example

Read Now →

Python Post Request with pem File Example

Read Now →

Python Post Request with Bearer Token Example

Read Now →

Python Get File Extension from Filename Example

Read Now →

Python Get First Date of Last Month Example

Read Now →

Python Get First Date of Next Month Example

Read Now →

Python Create Zip Archive from Directory Example

Read Now →

Python List All Dates Between Two Dates Example

Read Now →

How to Add Minutes to DateTime in Python?

Read Now →