40)))
A binary file “STUDENT.DAT” has structure (admission_number, Name, Percentage). Write a function countrec() in Python that would read contents of the file “STUDENT.DAT” and display the details of those students whose percentage is above 75. Also display number of students scoring above 75%
Answers
It goes like (consider the attachment if you're having trouble viewing the latex below) :
What have I done in the code?
i) Binary files (.dat or .bin) don't work like regular text file and a module 'pickle' is imported to perform operations it.
ii) pickle.load() accepts an argument as a file object and read the binary content within.
iii) Since you mentioned the data is stored in (admission_number, Name, Percentage) format that means percentage of each student must be at index 2 to access that I used i[2] and checked it for a condition.
Question:
A binary file “STUDENT.DAT” has structure (admission_number, Name,
Percentage). Write a function countrec() in Python that would read contents
of the file “STUDENT.DAT” and display the details of those students whose
percentage is above 75. Also display number of students scoring above 75%.
Solution:
import pickle
def CountRec():
fobj=open("STUDENT.DAT","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if rec[2] > 75:
print(rec[0],rec[1],rec[2],sep="\t")
num = num + 1
except:
fobj.close()
return num
LearnMore on brainly.in:
Write a function in Python POP(Arr), where Arr is a stack implemented by a
list of numbers. The function returns the value deleted from the stack.
https://brainly.in/question/27632437