Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Python , I\'m basically needing to change this to where it converts the text

ID: 3851223 • Letter: I

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 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:\Usersuser\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()

Explanation / Answer

import json

outfile = open('C:\Usersuser\Desktop\output.txt','w')
arr = []
with open('C:\Users\user\Desktop\payroll.txt', 'r') as f:
    for line in f:
        # split around space and then strip spaces from the ends
        columns = map(lambda s: s.strip(), line.split(' '))
        id=columns[0]
        name=columns[1] + ' ' + columns[2]
        wage=float(columns[3])
        days=columns[4:]
        totalhours=0
        days = columns[4:]
        for hour in days:
            totalhours=totalhours + float(hour)
            averageHours=totalhours/len(days)
            totalPay=totalhours*wage
        # adding set of items to arr list
        arr.append({
            "id": id,
            "name": name,
            "wage": wage,
            "days": days,
            "totalHours": totalhours,
            "averageHours":averageHours,
            "totalPay":totalPay
        })
#converting list to jsonString
jsonData = json.dumps(arr, indent=2)
#converting jsonString to jsonObject
jsonData = json.loads(jsonData)

# Iterating over the each set
for row in jsonData:
    result = row["name"]+' ID'+row["id"]+' worked '+str(row["totalHours"])+' hourly pay $'+str(row["wage"])+' hours: ' + str(round(row["averageHours"],2)) + '/day Total Pay: $' + str(row["totalPay"])
    print(result)
    # Writing each line to output file
    output.write(result+' ')
  
# Closing output file
outfile.close()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote