Advertisements
Advertisements
Question
Write a program in Python that defines and calls the following use of defined functions:
COURIER_ADD()
: It takes the values from the user and adds the details to a CSV file 'courier.csv
’. Each record consists of a list with field elements ascid
,s_name
,Source
,destination
to store Courier ID, Sender name, Source, and destination address, respectively.COURIER SEARCH()
: Takes the destination as the input and displays all the courier records going to that destination.
Code Writing
Solution
import csv
def COURIER_ADD():
cid=""
sname=""
src=""
dest=""
f= o p e n ( “ c o u r i e r .csv”,"w”,newline=")
courierwriter=csv.writer(f)
ans='y'
courierrec=[]
while ans=='y':
cid=input(“Enter courier id :")
sname=input(“Enter sender name:")
src= input(“Enter source :")
dest= input(“Enter destination :")
rec=( cid,sname, src,dest)
courierrec.append(rec)
ans=input(“Continue(y/n)")
courierwriter.writerows(courierrec)
f.close()
def COURIER_SEARCH():
d=""
ds=""
ds=input(“Enter destination to search :")
with open(“courier.csv”, "r", newline=")
as fh:
creader=csv.reader(fh)
for rec in creader:
d=str(rec[3])
if d==ds:
print(rec)
fh.close()
COURIER_ADD()
COURIER_SEARCH()
shaalaa.com
Opening and Closing a CSV File in Python
Is there an error in this question or solution?