In: Computer Science
This is a python file
Employee Roster
ID First Name Last Name Birth Date Age Hire Date Yrs Worked Salary
111 Joe Jones Sep 1, 1980 39 Oct 19, 1999 21 95000
222 Sam Smith Jul 10, 1956 63 Jan 1, 2000 20 50000
333 Rick Robins Aug 1, 1960 59 Aug 20, 1998 22 100000
Employees over 60 years old:
Sam Smith
Employees with salaries above 50000:
Joe Jones
Rick Robins
Employees that have worked for the company more than 20 years
Joe Jones
Rick Robins
SOLUTION - EDIT: CODE REFACTORED TO INCLUDE FUNCTIONS: Below is a screen shot of the python program to check indentation.Below is the output of the program: Console output:
Output file(emp_output.txt):
Below is the code to copy: #CODE STARTS HERE----------------
import os
from datetime import datetime
def read_file(): #Reads filename from input and stores content in empRoster
empRoster = []
f_name = input("Enter file name: ")
if not os.path.exists(f_name): #Check if file exists
print("File doesnot exist!")
exit()
with open(f_name) as f: #Read file
for line in f.readlines(): #Loop line by line
new_list = []
for col in range(len(line.strip().split(", "))):#Loop value by value
temp_li = line.strip().split(", ")
if col == 3 or col == 4: #For birth date and hire date, convert str to datetime
x = datetime.strptime(temp_li[col], '%m-%d-%Y')
new_list.append(x)
else:
new_list.append(temp_li[col])
empRoster.append(new_list)
return empRoster #Return emp's list of lists
def calculate(empRoster): #Calculates the employees who are above the given conditions
f_out = input("\nEnter a file name to output: ")
with open(f_out, "a+") as ff: #Write output to a file
ff.write("Employees over 60 years old:\n")
for emp in empRoster:
if emp[4] >= 60:
ff.write(emp[1] + " " + emp[2] + "\n")
ff.write("Employees with salaries above 50000:\n")
for emp in empRoster:
if int(emp[-1]) > 50000:
ff.write(emp[1] + " " + emp[2] + "\n")
ff.write("Employees that have worked for the company more than 20 years\n")
for emp in empRoster:
if int(emp[-2]) > 20:
ff.write(emp[1] + " " + emp[2] + "\n")
print("Output saved to file!")
def number_of_years(empRoster): #Calculates number of years since birth and since hire
newEmpRoster = [] #Stores new data to a new list
for line in empRoster:
newRoster = []
for col in line:
if line.index(col) == 3 or line.index(col) == 4: #Adds Age and yrs worked here
newRoster.append(datetime.strftime(col, '%m-%d-%Y'))
newRoster.append(int((datetime.now() - col).days / 365))
else:
newRoster.append(col)
newEmpRoster.append(newRoster)
return newEmpRoster
def print_report(empRoster): #Prints value in empRoster
print("{:^80}".format("Employee roster"))
print("{:6}{:15}{:13}{:20}{:5}{:15}{:15}{:15}".format("ID", "First name", "Last name",
"Birth date", "Age", "Hire date",
"Yrs worked", "Salary"))
for line in empRoster: #Formats output and prints reslt
print("{:6}{:15}{:13}{:20}{:^5}{:15}{:^15}{:15}".format(*line))
def main():
empRoster = read_file()
empRoster = number_of_years(empRoster)
print_report(empRoster)
calculate(empRoster)
main()
#CODE ENDS HERE------------------
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------