Computer Science
A binary file "salary.Dat" has structure [employee id, employee name, salary]. Write a function countrec() in Python that would read contents of the file "salary.Dat" and display the details of those employees whose salary is above 20000.
Answer
Let the "salary.dat" file contain following sample data:
[[101, 'Aditi', 25000], [102, 'Jatin', 19000], [103, 'Minaz', 28000]]
import pickle
def countrec(file_name):
f1 = open(file_name, 'rb')
while True:
try:
employees = pickle.load(f1)
for employee in employees:
if (employee[2] > 20000):
print("Employee ID:", employee[0], "Name:", employee[1], "Salary:", employee[2])
except EOFError:
break
f1.close()
countrec("salary.dat")
Output
Employee ID: 101 Name: Aditi Salary: 25000
Employee ID: 103 Name: Minaz Salary: 28000
Related Questions
Consider the following table STUDENT.
NO NAME AGE DEPARTMENT FEE SEX 1 PANKAJ 24 COMPUTER 120 M 2 SHALINI 21 HISTORY 200 F 3 SANJAY 22 HINDI 300 M 4 SUDHA 25 HISTORY 400 F Write a Python code to search a record as per given NO (number) using MySQL connectivity and print the data.
Write a Python code to insert a new record as per given table Student (No, Name, Age, Department, Fee, Sex) using MySQL connectivity.
A departmental store "ABC" is considering maintaining their inventory using SQL to store data and maintain basic transactions. As a database manager, Mehak has to create two tables as Customer & Transaction:
Table: CUSTOMER
CNo CNAME ADDRESS 101 Richa Jain Delhi 102 Surbhi Sinha Chennai 103 Lisa Thomas Bengaluru 104 Imran Ali Delhi 105 Roshan Singh Chennai Table: TRANSACTION
Dept CNO AMOUNT TYPE DOT T001 101 1500 Credit 2017-11-23 T002 103 2000 Debit 2017-05-12 T003 102 3000 Credit 2017-06-10 1004 103 12000 Credit 2017-09-12 T005 101 1000 Debit 2017-09-05 (i) Identify the attribute best suited to be declared as a Primary key in customer table.
(ii) Help Mehak to display details of all transactions of TYPE Credit from Table TRANSACTION.
(iii) Mehak wants to display all CNO, CNAME and DOT (date of transaction) of those CUSTOMERS from tables CUSTOMER and TRANSACTION who have done transactions more than or equal to 2000 for the Balance Sheet.
(iv) Mehak wants to display the last date of transaction (DOT) from the table TRANSACTION for the customer having CNO as 103. Which command will she use?
Sonia was writing a code to insert multiple rows in the cust.csv file. By mistake her younger sister removed some parts of it. Now she is totally confused about used module and other options:
from csv import writer with ............... ("cust.csv", "a", newline = "\n") as f: #line 1 dt = writer(...............) #line 2 while True: #line 3 sno= int (input ("Enter Serial No: ") ) cust_name = input ("Enter customer name: ") city = input ("Enter city: ") amt = int (input ("Enter amount: ") ) dt. ............... ( [sno, cust name, city, amt] ) #line 4 print ("Record has been added.") print ("Want to add more record?Type YES!!!") ch = input() #line 5 ch = ch.upper() if ch=="YES": print("*") else: breakAttempt any 4 out of the given questions:
(i) Name the method Sonia should use in Line 1.
(ii) Which object Sonia should use to connect the file in Line 2?
(iii) What is the use of Line 3?
(iv) Name the method she should use in Line 4.
(v) What is the use of Line 5?