Python Programming CSV Reader

Python Programming CSV File Reader

What is CSV ?

CSV means comma separated values and it is one of the most common format to import and export in spreadsheets and databases.

csv module has the functionalities to read or write the CSV format data.

CSV file delimiter must be a single character suppose csv file uses different character instead of ‘,’.

Let us consider example csv file ‘test-file.csv’ and has the below content.

id,name,class                                                                   
1,student1,VII                                                                  
2,student2,XI                                                                   
3,student3,VIII 

Python Programming CSV Reader

This python program is used to read the csv file using reader function by importing csv module and returns the each line as array

If any empty line in CSV file, returns the empty array [].

import csv                                                             
     
with open('test-file.csv', ‘r’) as csv_file:                                         
    csv_obj = csv.reader(csv_file, delimiter=',')                               
    for row in csv_obj:                                                         
            print(row)
Output:
$ python csv-reader.py
['id', 'name', 'class']
['1', 'student1', 'VII']
['2', 'student2', 'XI']
['3', 'student3', 'VIII']

Python Programming CSV File Reader using DictReader

This python program is used to read the csv file using DictReader function by importing csv module and returns the each line as dictionary using column name as keys.

Empty line in CSV file will not be considered here.

import csv                                                                      
                                                                                
with open('test-file.csv', ‘r’) as csv_file:                                         
    csv_obj = csv.DictReader(csv_file, delimiter=',')                           
    for row in csv_obj:                                                         
        print(row)

Output:
$ python csv-reader1.py
{'class': 'VII', 'id': '1', 'name': 'student1'}
{'class': 'XI', 'id': '2', 'name': 'student2'}
{'class': 'VIII', 'id': '3', 'name': 'student3'}

Python Programming CSV Reader to Search Row

This python program is used to find name in the particular row matching student id using reader method importing csv module.

import csv                                              

name_str = ''                                                                   
student_id = int(input("Enter id to search row: "))                             

with open('test-file.csv', ‘r’) as csv_file:                                         
    csv_obj = csv.reader(csv_file, delimiter=',')                               
    for row in csv_obj:                                                         
        if row[0] == str(student_id):                                           
            name_str = row[1]                                                                                                                              

print(name_str) 

Output:
$ python csv-reader.py
Enter id to search row: 2
student2

Another approach to find name in the particular row using DictReader in csv module.

import csv                                                                        

name_str = ''                                                                   

student_id = int(input("Enter id to search row:"))                              

with open('test-file.csv', ‘r’) as csv_file:                                         
    csv_obj = csv.DictReader(csv_file, delimiter=',')                           
    for row in csv_obj:                                                         
        if row['id'] == str(student_id):                                        
            name_str = row['name']                 

print(name_str)  

Output:

$ python csv-reader1.py

Enter id to search row:1

student1

Python CSV Reader with custom delimiter

Let us consider the sample CSV file with delimiter ‘;’

id;name;class                                                                   
1;student1;VII                                                                  
2;student2;XI                                                                   
3;student3;VIII

Need to use delimiter parameter is ‘;’ to parse the above csv file data and this python program is used to the student name in the row matching input id.

import csv                                             

name_str = ''                                                                   

student_id = int(input("Enter id to search row: "))                             
with open('test-file.csv', 'r') as csv_file:                                    
    csv_obj = csv.reader(csv_file, delimiter=';')                               
    for row in csv_obj:                                                         
        if row[0] == str(student_id):                                          
            name_str = row[1]                                           

print(name_str)    

Output:
$ python csv-reader.py
Enter id to search row: 1
student1

Privacy Policy  |  Copyright@2017 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Free online programming tutorials

Email Facebook Google LinkedIn Twitter
^