The following Python function is designed to search for the marks of a student…

2026

The following Python function is designed to search for the marks of a student in a CSV file named result.csv. The file contains records in the format RollNo, Marks. The function Search(RollNo) receives a Roll Number as an argument, traverses the CSV file, and displays the marks if a match is found.

(A)

def Search(RollNo):
    Found = False
    try:
        with open("result.csv", (B), (C)) as File:
            Records = (D)
            for Row in Records:
                if Row[0] == RollNo:
                    print("Roll No:", Row[0])
                    print("Marks:", Row[1])
                    Found = True
                    break
            if not Found:
                print("Record not found.")
    except FileNotFoundError:
        print("The file does not exist.")

(A) Write the statement for Blank (A) to import the built-in Python module used for CSV file operations.

(B) In Blank (B), which argument should be passed to the open() to specify the primary purpose (mode) of opening the file?

(C) In Blank (C), which argument should be passed to the open() function to ensure that newline characters are handled correctly across different platforms?

(D) Provide the method call for Blank (D) to create a reader object using the file pointer File.

Show answer & explanation

(A) Import statement (CSV module):

  • The built-in module used for CSV file operations is:
    import csv

(B) Mode to open file:

  • The file is to be read, so the mode is:
    "r" (read mode)

(C) Argument for handling newline properly:

  • To avoid extra blank lines across platforms, use:
    newline=""

(D) Method to create reader object:

  • The reader object is created using:
    csv.reader(File)

Explanation:

  • csv module provides functions to handle CSV files.

  • File is opened in read mode ("r") to access records.

  • newline="" ensures correct handling of newline characters.

  • csv.reader(File) reads the file row by row as a list.

Final Answers:

  • (A) import csv

  • (B) "r"

  • (C) newline=""

  • (D) csv.reader(File)

Explore the full course: Hpsc Pgt Computer Science