Q.A csv file "P_record.csv" contains the records of patients in a hospital. Each record of the file contains the following data : - Name of a patient - Disease - Number of days patient is admitted - Amount For example, a sample record of the file may be : ["Gunjan","Jaundice",4,15000] Write the following Python functions to perform the specified operations on this file :
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 →This is a CSV file-handling problem requiring two functions: one to filter and display records of 'Cancer' patients, and another to count total records. Both use Python's csv module to read structured data from "P_record.csv".
The problem tests your ability to work with CSV files in Python—a fundamental skill for handling tabular data. CSV (Comma-Separated Values) files store records where each line is a row and commas separate the fields. Python's csv module provides a clean interface to read such files row by row.
The first function needs to open the file, iterate through each record, check if the disease field matches 'Cancer', and display those records. The second function simply counts how many records exist by iterating through all rows. Both operations require opening the file in read mode and using a CSV reader object.
Solution
1. Import the necessary module
We need Python's built-in csv module to handle CSV file operations:
import csv
2. Function (i): read_data() to display Cancer patients
This function opens "P_record.csv", reads each record, and displays only those where the disease is 'Cancer'. Each record is a list with four elements: [Name, Disease, Days, Amount], so we check record[1] (the disease field).
def read_data():
with open("P_record.csv", "r") as file:
reader = csv.reader(file)
print("Details of Cancer patients:")
print("-" * 60)
for record in reader:
if record[1] == "Cancer":
print(f"Name: {record[0]}, Disease: {record[1]}, Days: {record[2]}, Amount: {record[3]}")
The with statement ensures the file closes automatically. We use csv.reader(file) which returns an iterator yielding each row as a list. The condition record[1] == "Cancer" filters for the disease field.
Using with open(...) is preferred over manual file.open() and file.close() because it handles cleanup automatically, even if an error occurs.
3. Function (ii): count_rec() to count total records
This function reads through the entire file and counts how many records exist. We initialize a counter to zero and increment it for each row:
def count_rec():
with open("P_record.csv", "r") as file:
reader = csv.reader(file)
count = 0
for record in reader:
count += 1
return count
Each iteration of the loop represents one record, so the final value of count gives the total number of records.
If your CSV file has a header row (column names in the first line), you need to skip it using next(reader) before counting, or subtract 1 from the final count. The problem statement doesn't mention headers, so we assume none exist.
4. Alternative approach for counting (more Pythonic)
You can count records more concisely using sum() with a generator expression:
def count_rec():
with open("P_record.csv", "r") as file: …
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.