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.
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
Related Questions
Anant has been asked to display all the students who have scored less than 40 for Remedial Classes. Write a user-defined function to display all those students who have scored less than 40 from the binary file "Student.dat".
Following is the structure of each record in a data file named "PRODUCT.DAT".
{"prod_code": value, "prod_desc": value, "stock": value}
The values for prodcode and proddesc are strings and the value for stock is an integer.
Write a function in Python to update the file with a new value of stock. The stock and the product_code, whose stock is to be updated, are to be inputted during the execution of the function.
Write statements to open a binary file C:\Myfiles\Text1.txt in read and write mode by specifying file path in two different formats.
Write a statement in Python to perform the following operations:
(a) To open a text file "BOOK.TXT" in read and append mode
(b) To open a text file "BOOK.TXT" in write mode
(c) To open a text file "BOOK.TXT" in append mode