Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In an attempt to change code, in PYTHON , that converts text.file output into a json file I'm getting some errors I need help with:

In an attempt to change code, in PYTHON, that converts text.file output into a json file I'm getting some errors I need help with:

line 9, I'm getting error: TypeError: 'map' object is not subscriptable. and line 39, I'm getting undefined variable: output

upon correction, could you make sure it works.

here is a copy and pasted version of the code below, then a screenshot from the IDE:

import json

outfile = open('C:\\Users\user\\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()

image text in transcribed

1 import json 3 outfilopen'C:1Usersluser Desktoploutput.txt'w' with open ( 'C: \ \ Usersi luserWDesktopWpayroll . txt . , 'r) as f : for line in f: t split around space and then strip spaces From the ends columns-map(Lanbd s; s.strip(), line.split(' )) id-columns [8] nane-columns[1]+'4 columns[2] 10 12 13 14 15 16 days-columns [4:] totalhours-0 days colunns[4:] for hour in days totalhours-totalhoursfloat hour) averageHours=tota!hours/1en(days) totalPay-totalhours*wage 18 19 20 21 t adding set of items to arc 1i arr.append( id": id, name": name, 23 24 25 26 27 7B 29 converting list to jsonString 30 isonData - json.dumps(arr, indent-2) 31 converting isonstring to isonUbiect 32 jsonDatajson.loads(jsonData) days": days, totalHours": totalhours averageflours": averagellours, totalPay":totalPay # Iterating over the each set for row in jsonData: 34 35 36 37 ]+ ID +row["id"]+' orked +str(rok("totalhours ]+' hourly pay $ +str(row[ age ])+ hours : + str(round(rov[ ],2)) + /day Jotal pay: S + str(ro [ tota result-rok("name print(result) average ours put file 48 41 Closing output file 12 outfile.close()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

New development is making our suburban sprawl become even worse.

Answered: 1 week ago

Question

What is meant by organisational theory ?

Answered: 1 week ago

Question

What is meant by decentralisation of authority ?

Answered: 1 week ago

Question

Briefly explain the qualities of an able supervisor

Answered: 1 week ago

Question

Define policy making?

Answered: 1 week ago