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:

csv File

Binary File

Stores information in the form of a text file.

Stores information in the form of 0s and 1s.

Human readable

Not human readable

Extension is .csv

Extension is .dat

Code:

import csv

 

def add ():

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

    write=csv.writer(f)

    fid=int(input( "Enter furniture id :: " ))

    fname=input( "Enter furniture name :: " )

    fprice=int(input( "Enter furniture price :: " ))

    list=[fid,fname,fprice]

    write.writerow(list)

    f.close()

 

def search ():

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

    records=csv.reader(f)

    flag=False

    print( "The Details of furnitures with price greater than 10,000: " )

    for r in records:

        if int(r[ 2 ])> 10000 :

            found=True

            print(r[ 0 ],r[1],r[2])

    if found==False:

        print( "Record not found" )

    f.close()

 

add()

add()

add()

search()

Class 12 - Sample Paper - Computer Science 2023.jpg

 

Remove Ads
Teachoo · Class 12 Explore Class 12
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.