It is important to close the file before exiting as Python makes sure that any
unwritten or unsaved data is flushed off to the file before it is closed.
import csv
def Add_Book():
f1=open("Book.csv","a",newline="\n")
writ=csv.writer(f1)
book_ID=int(input("Enter the Book id"))
B_name=input("Enter the Book Name")
pub=input("Enter the Publisher Name")
detail=[book_ID, B_name,pub]
writ.writerow(detail)
f1.close()
def Search_Book ():
f1=open("Book.csv","r") # ignore newline
detail=csv.reader(f1)
name=input("Enter the Publisher Name to be searched")
pub_count=0
for i in detail :
if i[2]==name:
pub_count+=1
print("NUMBER OF BOOKS: ",pub_count)
Add_Book()
Search_Book()
CSV files
? can be viewed in spreadsheets
? module CSV has to be imported
Text files
? can be viewed in the text editor
? No specific module required to be imported
import csv
def COURIER_ADD() :
f1=open("courier.csv","a",newline="\n")
writ=csv.writer(f1)
cid=int(input("Enter the Courier id"))
s_name=input ("Enter the Sender Name")
Source=input("Enter the Source Address")
destination=input("Enter Destination Name")
detail=[cid,s_name,Source,destination]
writ.writerow (detail)
f1.close()
def COURIER_SEARCH() :
f1=open("courier.csv","r") # ignore newline
detail=csv.reader(f1)
name=input("Enter the Destination Name to be searched")
for i in detail :
if i[3]==name:
print("Details of courier are: ",i)
COURIER_ADD()
COURIER_SEARCH()