In Software Application development time taken by a computer program in retrieving data from a file should be as optimum as possible. CSV(Comma Separated Value) file format is one out of the widely used formats to store data records.
In this blog, I will share the programming code written in Python which will illustrate following things:
- Read a CSV File.
- Retrieve its values in the console.
- Calculate total number of rows in a CSV File.
- Calculate time taken to read all those entries in a CSV File.
import csv, time def csvReader(): rowCount=0 with open('businessfile.csv') as csvfile: readCSV = csv.reader(csvfile, delimiter=',') for row in readCSV: print(row) rowCount+=1 print('There are %d rows in the CSV file.'%rowCount) initialTime = time.time() cr=csvReader() finalTime= time.time() print('The CSV Reader took %s seconds to read the file.' %(finalTime - initialTime))
Two python modules csv and time are used in above program code. Let’s have a general walk through of the code below.
- csv and time modules/libraries are imported with the import statement. L1
- A csvReader function is defined. L2
- Initially the row count of csv file is set to 0. L3
- CSV File businessfile.csv is opened and csv.reader reads the csv file. L4-L5
- A for loop prints each row of the CSV File in your console and rowCount is incremented by 1. L6-L8
- time.time() records your current system time. two variables initialTime are finalTime are defiend with the calue of current timestamp before and after the csvReader executing funciton respectivels. L10-L12
- A time difference is calculated in the last line L14.
I created a dummy csv file whose file size is 15MB. CSV file has following headers and similar data are added:
description,industry,level,size,line_code,value Business plans,manufacture,0,619 employees,D0201_01,18822
You can fidn the complete code and the csv file here in my github repository: github.com/sigdelsanjog/funwithpy
Running the code python3 csvreader.py I get following output:
There are 111111 rows in the CSV file. The CSV Reader took 6.583252906799316 seconds to read the file.