Code…
Question Answered step-by-step Code… Code==========================================================================def open_file(filename, mode): try: if mode == “rt”: with open(filename, mode) as file: data = file.read() return data if mode == “wt”: with open(filename, mode) as file: return file except FileNotFoundError: print(“nUnable to open”, filename) print(“nGoodbye”) return None def create_road_name_dict(filename): road_dict = {} road_list = filename.split() i = 1 while i < len(road_list): x = road_list[i].split(",") key = x[0] value = x[1] + " " + road_list[i + 1] road_dict[key] = value i += 2 return road_dict ==========================================================================Using Python: How do I make the following function using the the code I supplied above? make a function named create_traffic_averages_dict, which accepts one argument: the extracted data from the speed data file, which is returned by the open_file function. It returns a dictionary that maps segment identifiers (as strings) to average traffic speeds (as floats). Segment identifiers and speeds must be extracted from the speed file data. The average speeds must be rounded to 2 decimal places. and make a function named write_report, which has three parameters: a file object to write the data to, and the road names dictionary and the average traffic speeds dictionary from the previous functions. The function writes averaged traffic speeds to a file formatted as commaseparated values. This function must first write the field names 'roadname,average' to the file, and then on subsequent lines, it must write the capitalized road 3 name, a single comma, and the average speed, following the format shown on the right. Data lines must be sorted by segment identifier. After the report is written, the message 'Report written to a file.' is displayed to the user. Datasets:Image transcription textdata.txt - Notepad File Edit Format View segmentID, speed 3,43.99 km/h 9,46.34 km/h 3,39.24km/h 1, 58.08 km/h 4, 47.45 km/h 4, 42.83 km/h 5,36.81 km/h 1, 44.52 km/h 4, 48.62 km/h 8,52.9km/h 6, 40.65 km/h 7,33.76 km/h 6, 31.09 km/h 0, 45.82 km/h 9,56.99 km/h 9, 45.1 ... Show more... Show moreImage transcription textnames.txt - Notepad File Edit Format View segmentID, name 4,55 ave 6,49 st 2,53 ave 9,52 st5,48 st 8,51 st 0, 51 ave 1,52 ave 7,50 st 3,54 ave... Show more Computer Science Engineering & Technology Python Programming COMPSCI MISC Share QuestionEmailCopy link Comments (0)


