A binary file Employee.dat has structure (empno, ename, salary). Write a function Find() in Python that would read contents of the file Employee.dat and display details and count of those employees whose name begin with A or S)
Answers
Answer:
def Find():
import pickle as p
with open("Employee.dat","rb") as r:
y=p.load(r)
print(y)
c=0
for i in y:
if i[1][0]=="A" or i[1][0]=="S":
print(i)
c=c+1
print("Number of employees with A or S at start of name=",c)
Find()
Explanation:
Given:
A binary file with Emplyee.dat having structure (empno, ename, salary)
To Find:
In Python, that would read the file Emplyee.dat and display the details and count the number of employees whose name begin with A and S
Solution:
For reading the file,
import pickle
def addrec():
fobj=open("Emplyee.dat","wb")
eid=input("EnterE.Id")
ename=input("Enter Name")
designation=input("Enter designation")
salary=int(input("Enter salary")
I=[eId,ename,designation,salary]
pickel.dump(I,f1)
f1.close()
To Count the number of members with A and S as staring letters
Add,
test_list = [ 'Sanu' , 'Akash' , 'Nikil' , 'Manjeet' ]
check = 'A' , 'S'
print("The original test: " + str(test_list))
res = [idx for idx in test_list if idx[0].lower() == check.lower()]
print("The list of match first letter: " + str(res))
#SPJ3