Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

What is the advantage of using a csv file for permanent storage?

Write a Program in Python that defines and calls the following user defined functions:

(i) ADD() – To accept and add data of an employee to a CSV file ‘record.csv’. Each record consists of a list with field elements as empid, name and mobile to store employee id, employee name and employee salary respectively.

(ii) COUNTR() – To count the number of records present in the CSV file named ‘record.csv’.

 

Answer:

Advantages of using a csv file for permanent storage are:

  • It is human readable and easy to edit.
  • It is compact.
  • It is faster to handle.

Code:

import csv

 

def ADD ():

    f=open( 'record.csv','a', newline= "\n" )

    write=csv.writer(f)

    empid=int(input( "Enter Employee id :: " ))

    name=input( "Enter name :: " )

    mobile=int(input( "Enter mobile number :: " ))

    list=[empid,name,mobile]

    write.writerow(list)

    f.close()

 

def COUNTR ():

    f=open( 'record.csv','r' ,newline= "\n" )

    records=csv.reader(f)

    print( "Number of records in the csv file:" ,len(list(records)))

    f.close()

 

ADD()

ADD()

COUNTR()

Slide6.JPG

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo