Counting the Number of Rows in a CSV File with Python

Counting the Number of Rows in a CSV File with Python

Table of contents

No heading

No headings in the article.

n this tutorial, you will learn how to write a Python program that reads a CSV file and displays the number of rows in it. We will be using the csv module to read the file and iterate through it to count the number of rows. Let's get started with the code.

To read a CSV file and count the number of rows in it, we will follow these steps:

  1. Import the csv module.
import csv
  1. Open the CSV file in read mode. In this example, we are using the file "test.csv".
with open("test.csv","r") as f:
  1. Create a reader object to read the file. Also, define a variable called count with an initial value of 0 to count the number of rows.
f_reader = csv.reader(f)
count = 0
  1. Iterate through the rows in the reader object. For each row, increment the count variable by 1.
for row in f_reader:
    count += 1
  1. Print out the value of the count variable to see the number of rows in the CSV file.
print("Number of rows = ", count)

Here is the complete code for this program:

import csv

# Open the CSV file in read mode
with open("test.csv","r") as f:
    # Create a reader object to read the file
    f_reader = csv.reader(f)
    # Define a variable called 'count' with an initial value of 0 to count the number of rows
    count = 0
    # Iterate through the rows in the reader object
    for row in f_reader:
        # Increment the 'count' variable by 1 for each row
        count += 1

# Print out the number of rows in the CSV file
print("Number of rows = ", count)

Running this code will result in the following output:

Number of rows = 6

I hope this code and explanation are helpful in understanding how to read a CSV file and display the number of rows in it using Python. Thank you for reading!