Question

Pay 65 35 43 19.43 Hours 8.75

How to read this csv file and compute the regular time pay and overtime pay. In Python please.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question.
Here is the completed code for this problem. Comments are included so that you understand whats going on. Let me know if you have any doubts or if you need anything to change.
Thank You !!

Update the below line to point to the csv file in your laptop.

filename='F:\\pay.csv'


=============================================================================

import os # to check if the file is valid
def read_csv_file(filename):
    if os.path.isfile((filename)) is not True:return None
    with open(filename,'r') as infile:
        employee_data=[] # reads the data from file and store it in the list
        for line in infile.readlines()[1:]:
            line=line.strip().split(',')
            line=[float(data) for data in line if len(data)>0]
            employee_data.append(line)
    return employee_data # returns the data to the main function 

def main():
    filename='pay.csv' # update this line to point to the csv file
    employee_data = read_csv_file(filename)
    if employee_data==None:
        print('Unable to read file :{}, Please check the file is valid.'.format(filename))
        return
    print('{0:<15} {1:<15} {2:>15} {3:>15} {4:>15}'.format('Regular Hrs','Overtime Hrs','Pay Rate',\
                        'Regular Pay','Overtime Pay'))
    for employee in employee_data:
        regular_hrs=employee[0] 
        overtime_hrs=0
        if regular_hrs>40: # calculate overtime hours
            overtime_hrs=regular_hrs-40
            regular_hrs=40
        rate=employee[1]
        print('{0:<15.2f} {1:<15.2f} {2:>15.2f} {3:>15.2f} {4:>15.2f}'.\
          format(regular_hrs,overtime_hrs,rate,regular_hrs*rate,overtime_hrs*rate*1.5))

main()

====================================================================================media%2F4fe%2F4fe69f85-298c-465a-a0ff-bf

media%2F5c2%2F5c250508-76de-4284-8c6c-52

Add a comment
Know the answer?
Add Answer to:
How to read this csv file and compute the regular time pay and overtime pay. In Python please. Pay 65 35 43 19.43 Hours 8.75 Pay 65 35 43 19.43 Hours 8.75
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT