Looping over a text file list with python

后端 未结 5 1398
予麋鹿
予麋鹿 2021-01-29 10:16

EDIT: Have updated post for better clarity, no answers have yet to help!

Alright, so my assignment is to take a text file, that would have 4 entries per line, those bein

5条回答
  •  终归单人心
    2021-01-29 10:29

    import os.path
    import sys
    #fileQuestion = input("What is the name of your file?: ")
    fileQuestion = "Testfile.txt"
    heading1 = "{0:15s}{1:15s}{2:10s}{3:15s}{4:20s}{5:15s}".format("First Name",   "Last Name", "Hours", "Payrate", "Overtime Hours", "Gross Pay")
    heading2=    "=============================================================================================================="
    
    print(heading1)
    print(heading2)
    
    if os.path.isfile(fileQuestion) == True:
        file_handle = open(fileQuestion, 'r')
    #file = open('emps', 'r')
    #data = file.readlines()    I would't go for readline here
    
    #file_handle2 = open('outupt.txt')
    total_gross_pay = 0
    number_of_employees = 0
    average_gross_pay = 0
    total_overtime = 0
    standard_working_hours = 40
    
    for i in file_handle:
        data = i.rstrip().lstrip().split()
    
        #print (data)
        first_name, last_name, hours, payrate = data
    
        hours = int(hours)
        payrate = int(payrate)
        basic_pay = hours * payrate
    
        if(hours > standard_working_hours):
            overtime = hours - standard_working_hours
            overtime_premium = overtime * payrate
            gross_pay = overtime_premium + basic_pay
    
        else:
            overtime = 0
            gross_pay = basic_pay
    
        total_overtime += overtime
        total_gross_pay += gross_pay
        number_of_employees += 1
    
        print("{0:15s}{1:15s}{2:10s}{3:15s}{4:20s}{5:15s}".format(first_name, last_name, str(hours), str(payrate), str(overtime), str(gross_pay)))
    
    print('\n')
    print("Total Gross Pay:   ",total_gross_pay)
    print("Average Gross Pay: ",total_gross_pay/number_of_employees)
    print("Total overtime:    ",total_overtime)
    

提交回复
热议问题