Question: In Python , I'm basically needing to change this to where it converts the text file as set in the code below into a json
In Python,
I'm basically needing to change this to where it converts the text file as set in the code below into a json file and does the following:
Formatting data as JSON data ( reformatting a minimum of 4 rows as JSON data )
Looping through JSON Data
___________________________________________________________________________________________________________________________
f = open("C:\\Users\\user\\Desktop\\payroll.txt", "r")
outfile = open('C:\\Users\user\\Desktop\\output.txt','w') output=[]
for line in f.readlines(): columns=line.split() id=columns[0] name=columns[1] + ' ' + columns[2] wage=float(columns[3]) days=columns[4:] totalhours=0
for hour in days: totalhours=totalhours + float(hour) averageHours=totalhours/len(days) totalPay=totalhours*wage
result=name+' ID'+id+' worked '+str(totalhours)+' hourly pay $'+str(wage)+' hours: ' + str(round(averageHours,2)) + '/day Total Pay: $' + str(totalPay)
print(result) output.append(result+' ')
# Iterating over each item in list variable for line in output: # Writing each line to output file outfile.write(line)
# Closing output file outfile.close()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
