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()