Advertisements
Advertisements
प्रश्न
Write a program in Python that defines and calls the following user-defined functions:
- Add_Book(): Takes the details of the books and adds them to a CSV file ‘Book. csv'. Each record consists of a list with field elements such as
book_ID, B_name andpubto store book ID, book name and publisher, respectively. - Search_Book(): Takes publisher name as input and counts and displays the number of books published by them.
कोड लेखन
Advertisements
उत्तर
import csv
def Add_Book():
bid=""
bname=""
pub=""
f=open(“Book.csv”,"w” newline=")
bookwriter=csv.writer(f)
ans="y’
bookrec=[]
while ans=='y':
bid=input(“Enter book id :")
bname=input(“Enter book name :")
pub= input(“Enter publisher :")
rec=( bid,bname,pub)
bookrec.append(rec)
ans=input(“Continue(y/n)”)
bookwriter.writerows(bookrec)
f.close()
def Search_Book():
count=0
pub=""
p=""
pub=input(“Enter publisher name to search :")
with open(“Book.csv”,"r”, newline =")
as fh:
breader=csv.reader(fh)
for rec in breader:
p=str(rec[2])
if p==pub:
count+=1
print(“No. of books published by “, pub,”=" count)
fh.close()shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
