KnowledgeBoat Logo

Computer Science

Given a binary file "STUDENT.DAT", containing records of the following type:

[S_Admno, S_Name, Percentage]

Where these three values are:

S_Admno — Admission Number of student (string)
S_Name — Name of student (string)
Percentage — Marks percentage of student (float)

Write a function in Python that would read contents of the file "STUDENT.DAT" and display the details of those students whose percentage is above 75.

Python Data Handling

9 Likes

Answer

Let the file "STUDENT.dat" include the following sample data:

[['101', 'Aishwarya', 97.0], 
['102', 'Sakshi', 85.0], 
['103', 'Prateek', 70.0]]
import pickle

def display_students(filename):
        file = open(filename, 'rb')
        student_records = pickle.load(file)
        above_75 = []
        for student in student_records:
            if student[2] > 75.0:
                above_75.append(student)
        if above_75:
            print("Students with percentage above 75:")
            for student in above_75:
                print("Admission Number:", student[0], "Name:", student[1], "Percentage:", student[2])
        file.close()
display_students("STUDENT.DAT")
Output
Students with percentage above 75:
Admission Number: 101 Name: Aishwarya Percentage: 97.0
Admission Number: 102 Name: Sakshi Percentage: 85.0

Answered By

2 Likes


Related Questions