Question: In Python, I have created a database list with a .txt file consisting of employee ID number, name, hourly wage and number of hours worked.
In Python, I have created a database list with a .txt file consisting of employee ID number, name, hourly wage and number of hours worked. As seen below in a sample portion. The first column is the ID, second is the first name, then second name, followed by hourly wage and then numbers of hours worked per day:
4123 Adam Carlson 12 15.0 8.0 1 3
5456 Barney Craig 22 4 11.6 5 12
7189 Catlyn Davis 10 8 7.5 2
2012 David Ellis 9 4.5 1.5 1
1345 Emma Franklin 28 40
1678 Dan George 15 6.5 2.0 3.5 4 5.0
1901 Phil Hall 10 9 8.5 7 6.5 5 1.5
8234 James Henry 17 0 0 0 1 0 0 4
2567 Lisa Lincoln 55 5.0 3.0 2.5 3
3890 Mike Watson 21 12.5 16.5 11
I figured out how to use:
f=open("nameoffile.txt","r+")
print f
to implant the code into python but I need to be able to "loop through the txt file one line at a time". Our instructor gave us these lines of sample code to place in the code to read it one line at a time but I can't figure out how to do the for loop:
For line in f:
for loop code
then do a column split like this below
columns = line.split()
id=columns[0]
name=columns[1] + ' ' + columns [2]
wage = float (columns[3])
days = columns[4: ]
I'm supposed to calculate the total pay for each employee and print the output to the screen and send a copy to the output file: The printed and file output information about each employee consists of employee id, full name, total hours worked, total pay, average hours worked each day
Sample Output to console (similar output is also sent to file) :
Adam Carlson ID 4123 worked 27.0hourly pay $12.0 hours: 6.75 / dayTotal Pay: $324.00
Barney Craig ID 5456 worked 32.6hourly pay $22.0 hours: 8.15 / dayTotal Pay: $717.20
Catlyn Davis ID 7189 worked 17.5hourly pay $10.0 hours: 5.83 / dayTotal Pay: $175.00
David Ellis ID 2012 worked 7.0hourly pay $9.0 hours: 2.33 / dayTotal Pay: $63.00
Emma Franklin ID 1345 worked 40.0hourly pay $28.0 hours: 40.00 / dayTotal Pay: $1120.00
Dan George ID 1678 worked 21.0hourly pay $15.0 hours: 4.20 / dayTotal Pay: $315.00
Phil Hall ID 1901 worked 37.5hourly pay $10.0 hours: 6.25 / dayTotal Pay: $375.00
James Henry ID 8234 worked 5.0hourly pay $17.0 hours: 0.71 / dayTotal Pay: $85.00
Lisa Lincoln ID 2567 worked 13.5hourly pay $55.0 hours: 3.38 / dayTotal Pay: $742.50
Mike Watson ID 3890 worked 40.0hourly pay $21.0 hours: 13.33 / dayTotal Pay: $840.00
I can't figure out how to read the data one line at a time and manipulate it to output to screen with calculated wages and hours plus how do you create a second output file in the process?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
