How to Read CSV File in Python Example

Websolutionstuff | Sep-18-2024 | Categories : Python

In this article, I'll show you how to read CSV files in Python. Using Python, we can easily read CSV files line by line with the help of the csv module. This is useful when you need to work with data stored in a structured format, like spreadsheets or databases. Let’s explore how to read and process CSV files step by step.

CSV files (Comma Separated Values) are simple text files where data is stored in a table format, making them a popular choice for data exchange. Python’s csv module provides built-in methods to handle these files efficiently. By the end of this article, you'll know how to read data from CSV files and use it in your Python programs.

How to Read CSV Files in Python

How to Read CSV File in Python

 

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.

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)

 

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)

 

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)

 


You might also like:

Recommended Post
Featured Post
How To Get Selected Checkbox List Value In Jquery
How To Get Selected Checkbox L...

In this tutorial, I will explain you to how to get the selected checkbox value from a checkbox list in jquery, If y...

Read More

Jun-17-2020

How to Import CSV File in Laravel 11 with Livewire 3
How to Import CSV File in Lara...

When I started working with Laravel 11 and Livewire 3, I wanted a simple way to import CSV files into my project. It sou...

Read More

Mar-24-2025

How to Install PHP DOM Extension in Ubuntu 23.04
How to Install PHP DOM Extensi...

In this tutorial, I will guide you through the process of installing the PHP DOM Extension in Ubuntu 23.04. The PHP DOM...

Read More

Jan-29-2024

Laravel 9 Create Zip File And Download
Laravel 9 Create Zip File And...

In this article, we will see laravel 9 create a zip file and download it. Laravel provides ZipArchive class fo...

Read More

May-02-2022