DEV Community

oladejo abdullahi
oladejo abdullahi

Posted on

How to read csv file in python.

Reading csv file

so how do we open and read csv file in python. before we proceed in to that i would advise you to read how to open and read file if you are new here so that you can understand better. so let's go into the lesson.

What is CSV ?

CSV means Comma Separated Values.it is the commonest import and export format for spreadsheets and databases which are always used as exchange of data between applications and popular data format in Data Science.
A CSV file stores data in a tabular form i.e each data field is separated by a delimiter(comma in most cases). for all csv file, it must be saved with the .csv file extension.

how to open CSV file.

To open csv file nothing diffferent from the way you open text file you can click here if you had no experience on how to open or read file before. So we open CSV file using open() function so we can set the mode to 'r' so that the file we open will be ready for reading or not even bother as open() is in 'r' mode at default.

Example

let say we have a csv file named payroll.csv looking like the following.
Payroll.csv
Alt Text
so the file above can be open for reading with the following code.

csvFile=open('payroll.csv','r')
Enter fullscreen mode Exit fullscreen mode

another way to open the file is using open the file with the with keyword like the following.

with open('payroll.csv','r') as csvFile
Enter fullscreen mode Exit fullscreen mode

the code above do the same thing as the previous one and it is even the better as you don't need to close file the file when you are done.

how to Reading from CSV file

After open the csv for for reading, Before we read the csv file we need to import a python module called csv for the handling of CSV files and the reader class from the module for reading data from a CSV file.

Example

import csv
csvFile= open('payroll.csv', 'r') # opening the CSV file 
the_file=csvFile.reader(file) # reading the CSV file  
for rows in the_file:
    print(rows)# displaying the contents of the CSV file
Enter fullscreen mode Exit fullscreen mode

Output:

['EMPL Number', 'EMPL Name', 'Hourly Rate', 'Hours Worked', 'Gross Pay', 'S.S Tax', 'Net Pay']
['E00001', 'Ford', '7.5', '35', '262.5', '15.75', '4134.375']
['E00002', 'Mino', '8', '30', '240', '14.4', '3456']
['E00003', 'Bell', '6.5', '25', '162.5', '9.75', '1584.375']
['E00004', 'Davis', '9', '40', '360', '21.6', '7776']
['E00005', 'Turro', '10', '39', '390', '23.4', '9126']
Enter fullscreen mode Exit fullscreen mode

another way of writing the code is :

import csv     
# opening the CSV file  
with open('payroll.csv', 'r') as csvfile:  

  # reading the CSV file  
 the_file = csv.reader(file)  

  # displaying the contents of the CSV file  
  for rows in the_file:  
        print(rows) 
Enter fullscreen mode Exit fullscreen mode

Output:

['EMPL Number', 'EMPL Name', 'Hourly Rate', 'Hours Worked', 'Gross Pay', 'S.S Tax', 'Net Pay']
['E00001', 'Ford', '7.5', '35', '262.5', '15.75', '4134.375']
['E00002', 'Mino', '8', '30', '240', '14.4', '3456']
['E00003', 'Bell', '6.5', '25', '162.5', '9.75', '1584.375']
['E00004', 'Davis', '9', '40', '360', '21.6', '7776']
['E00005', 'Turro', '10', '39', '390', '23.4', '9126']
Enter fullscreen mode Exit fullscreen mode

we just read the csv file. did you find it interesting? consider to follow me so that you don't miss the next lesson. keep on coding it is fun.

Top comments (0)