Q.(a) Write one difference between CSV and text files. Write a program in Python that defines and calls the following user defined functions:
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Part (a): CSV is delimited/tabular while text is free-form; COURIER_ADD() appends a record and COURIER_SEARCH() prints records matching a destination.
Part (b): a file must be closed so buffered data is flushed and resources freed; Add_Book() appends a record and Search_Book() counts a publisher's books.
Part (a)
One difference between CSV and text files. A CSV (Comma-Separated Values) file is structured: each line is one record and its fields are separated by a delimiter (a comma), so it reads like a table. A plain text (.txt) file is unstructured — it just holds characters with no fixed field layout.
We use Python's csv module. COURIER_ADD() opens the file in append mode ('a', so existing records are kept) with newline='' (avoids blank lines on Windows) and writes a four-field list. COURIER_SEARCH() reads every row and prints those whose destination field matches the input.
import csv
def COURIER_ADD():
with open('courier.csv', 'a', newline='') as f:
w = csv.writer(f)
cid = input("Courier ID: ")
s_name = input("Sender name: ")
Source = input("Source: ")
destination = input("Destination: ")
w.writerow([cid, s_name, Source, destination])
print("Record added.")
def COURIER_SEARCH():
dest = input("Destination to search: ")
found = False
with open('courier.csv', 'r', newline='') as f:
for rec in csv.reader(f):
if len(rec) == 4 and rec[3] == dest:
print(rec)
found = True
if not found:
print("No couriers for this destination.")
COURIER_ADD()
COURIER_SEARCH()
``` …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.