Python Program to Read a CSV File and Display the Number of Rows in it

Csv File:

The CSV (Comma Separated Values) format is the most used spreadsheet and database import and export format. Prior to attempts to standardise the format in RFC 4180, the CSV format has been in use for many years. Due to the lack of a well-defined standard, small differences in data produced and consumed by different applications are common. Processing CSV files from multiple sources can be inconvenient due to these variances. Despite the differences in delimiters and quotation characters, the general structure is comparable enough that a single module can quickly manage such data while hiding the specifics of reading and writing data from the programmer.

The csv module provides classes for reading and writing CSV tabular data. It enables programmers to say things like “put this data in the format favoured by Excel” or “read data from this file generated by Excel” without having to grasp the specifics of Excel’s CSV format. Programmers can also define their own special-purpose CSV formats or describe the CSV formats that other apps understand.

The reader and writer objects in the csv module read and write sequences. The DictReader and DictWriter classes allow programmers to read and write data in dictionary format.

Given a file, the task is to read a given CSV file and print the number of rows in the file in Python

Program to Read a CSV File and Display the Number of Rows in it in Python

Below is the full approach for reading a given CSV file and printing the number of rows in the file in Python.

Approach:

  • Import csv module using the import Keyword.
  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Get the lines of the csv file using the reader() function and store it in a variable.
  • Take a variable(which gives the no of rows in a given CSV file) and initialize its value with zero.
  • Iterate in rows of the above csv file using the for loop.
  • Inside the for loop, increment the above count value by 1.
  • Print the count of the number of rows in a given csv file.
  • The Exit of Program.

Below is the implementation:

# Import csv module using the import Keyword
import csv
# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "dummy_data.csv"
# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
with open(givenFilename, 'r') as givenfilecontent:
    # Get the lines of the csv file using the reader() function and store it in a variable
    filetext = csv.reader(givenfilecontent)
    # Take a variable(which gives the no of rows in a given csv file) and initialize its value with zero.
    cnt = 0
    # Iterate in rows of the above csv file using the for loop 
    for rows in filetext:
        # Inside the for loop, increment the above count value by 1
        cnt += 1
# Print the count of number of rows in a given csv file     
print("The count of number of rows in a given csv file = ", cnt)

Output:

The count of number of rows in a given csv file = 20

dummy_data.csv:

id calories protein fat
0 70 4 1
1 120 3 5
2 70 4 1
3 50 4 0
4 110 2 2
5 110 2 2
6 110 2 0
7 130 3 2
8 90 2 1
9 90 3 0
10 120 1 2
11 110 6 2
12 120 1 3
13 110 3 2
14 110 1 1
15 110 2 0
16 100 2 0
17 110 1 0
18 110 1 1

Explanation:

0 to 18 = 19 rows 
19 + 1 heading row = 20
so, Total no rows = 20

Google Colab Images:

Files and Code:

Leave a Comment