Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are providee with two text files. Thye first file, named 'Car_sales.txt, contains technical and commercial information about some cars. The following figuer is a

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
You are providee with two text files. Thye first file, named 'Car_sales.txt", contains technical and commercial information about some cars. The following figuer is a screenshot of the file. Columns are separated by a tab ('\t'). The first line represents the header while the following lines represent the data. The second file, named 'car_list.txt", contains the list of car names. Each name is written in a seprate file. Modele 3 12 W A A w www MW 11 w Nie Se . 0 1 Objectives: 1. Calculate the total and the average sales (in thousands) for each auto manufacturer (Task 1). Show the results as follows: Car name Total sales Average sales Acura 64.891 21.630333 Audi 40.557 13.519000 2 BMW 26.758 13.379000 Buick 242019 60.504750 Cadillac 112.178 22.435600 Chevrolet 554 365 61.596111 6 Chrysler 148 241 24.706833 7 Dodge 808 826 80.882600 Ford 1846.965 184.696500 3 4 5 A4 2 Find the best-selling car model for each car manufacturer (Task 2). Organize the results in a csv file. It should look like the following: Car name Best selling O Acura TL 1 Audi 2 BMW 5281 3 Buick Century 4 Cadillac DeVille 5 Chevrolet Cavalier 6 Chrysler Sebring Conv. 7 Dodge Ram Pickup 8 Ford F-Series 9 Honda Accord 10 Hyundai Elantra 11 Infiniti 130 12 Jeep Grand Cherokee 13 Lexus ES300 14 Lincoln Town car Solution 1. You are provided with the solution for the first task. 2 You must provide the solution for the second task. Note: It is recommended to consider exception handling Task 1 File names 1 data_file = 'Car_sales.txt 3 car_11st 'car_list.txt' Work Plan To solve the task 1 we need to create two functions: 1. The first function aims to get (or return) information about a specif car. . Make a test plan: Test the functionality of the function for some cars. 2 The second function aims to scan the list of all the cars then for each car it calls the first function to get its information Make a test plan: test the functionality of the fucntion for all the cars. 3. Save results in a scv format file. Define the 1st functiuon 2 def getDataAbout Car(in_data_file, in_car, in_key): This function find information about a specific car Input: in_data_filet name of the file containing data @in_cart name of the car @in_key: target information output: @ou_tots: total sales @ou_argst average sales 10/5/2020 DCS, SQU 4 5 6 7 8 9 le 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 *open data file file = open(in_data_file) # read the header (1st line) line . file.readline().strip().split("\t") # get the index (colum nunber) of the key information indx_key - line. Index(in_key) read data from next lines total_sales. # used to compute the total sales . # used to compute the number of car's model for line in file: line line.strip).split("\t") car_nane line() if car_nane. lower() -- car.lower(): total_sales total_sales float(1ine[indx_key]> average_sales - total sales/ ou_tots . total sales ou_avgs. average_sales # close file file.close() return ou_tots, ou_avgs Test for one car, i.e., Acura 1 2 car = Input("Which car manufacturer would you like to look up for sales information?") total sales. average sales Det DataAhout Caridata file.car. Sales in thousands 3 print("Total sales - 12.2f and Average sale - 2.24"{total_sales, average_sales)) which car manufacturer would you like to look up for sales information?acura Total sales - 64.89 and Average sale - 21.63 Define the 2nd function 2 3 4 5 6 7 ** 1 def computeTotalSalesbycarttanufacturer(in_data_file, In_car_list, in_key): The function compute the total sales for all the car manufacturers. Input: @in_data_file: data file @in_car_list: car list @in_key: target information Output: 9 ou_ds: dictinary of sale information ae 11 car_file.open(in_car_list, 'r') 12 dict sales - () 13 car_names - total_sales - 11 15 average_sales - 11 16 for car in car file: 17 car_nane car.strip().split()[0] 18 car_names.append(car_name) 19 # call the first function 2e tots, avgs - getdataaboutCar(in_data_file,car_name, in_key) 21 total_sales.append(tots) 22 average_sales.append(avgs) 23 dict_sales['Car name'] =car_names 24 dict_sales['Total sales') .total_sales 25 dict sales['Average sales') average sales 26 27 ou_ds - dict_sales 28 car_file.close() 29 return dict_sales Test for all cars 1 dict_sales - compute TotalSales@yCartlanufacturer(data_file, car_list, Sales_in_thousands 2print(dict_sales) Record the result in a CSV format file 1 Import pandas as pd 2. df = pd.DataFrame(dict_sales) 3df.head(5) # save it as csv file 5df.to_cs (salesbyCars.csv) 4 Task 2 def getBestsellingCar{s_data_file, l_car): 1 2. 3 4 1 2 car input("Enter the car manufacturer rane for which you want to find the best selling: 1 2 3 def bestsellingsyCarManufacturer(s_data_file, 1_car_list): 1 best selling all_cars. 2 print (best selling_all_cars)

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

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

1. Who will you assemble on the team?

Answered: 1 week ago