Question: using python Lab 3: Car Sales problem You are providee with two text files. Thye first file, named 'Car_sales.txt', contains technical and commercial information about
using python





Lab 3: Car Sales problem 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. 320 133 39157132 132 2 21 2012 31/2011 717120 www 23 16 24 gal 135 15. Eldorado 15 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 BMW 26.758 13.379000 Buick 242.019 60.504750 Cadillac 112.178 22.435600 Chevrolet 554.365 61.596111 Chrysler 148.241 24.706833 Dodge 808.826 80.882600 2 3 4 5 6 7 8 Ford 1846.965 184.696500 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 Acura TL 1 Audi A4 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' 2 car_list = '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 specifcar. 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 function 1 2 3 5 6 7 8 def getDataAboutCar(in_data_file, in_car, in_key): This function find information about a specific car input: @in_data_file: name of the file containing data @in_car: name of the car @in_key: target information output: @ ou_tots: total sales @ ou_avgs: average sales 10/5/2020 DCS, SQU 10 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 (column number) of the key infoirmation indx_key - line.index(in_key) # read data from next lines total_sales - # used to compute the total sales i- # used to compute the number of car's model for line in file: line - line.strip().split("\t") car_name - line[@] if car_name.lower() -- car.lower(): total_sales - total_sales + float(line indx_key]) i - 1+1 average_sales - total_sales/i 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 - getDataAboutCar(data file.car. 'Sales in thousands') 3 print("Total sales = %.2f and Average sale - %2.2f%(total_sales, average_sales)) D 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 def compute TotalSalesByCarManufacturer(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: @ ou_ds: dictinary of sale information 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 car_file - open(in_car_list, 'r') dict sales - 1 car_names - [] total_sales - [ ] average_sales - [] for car in car_file: car_name - car.strip().split([e] car_names.append(car_name) # call the first function tots, avgs - getDataAboutCar(in_data_file,car_name, in_key) total_sales.append(tots) average_sales.append(avgs) dict_sales['Car name' ] - car_names dict_sales [ 'Total sales'] = total_sales dict_sales['Average sales'] = average_sales ou_ds - dict_sales car_file.close() return dict_sales Test for all cars 1 dict_sales - computeTotalSalesByCarManufacturer(data_file, car_list, 'Sales_in_thousands" 2 print (dict_sales) Record the result in a CSV format file 1 import pandas as pd 2 df = pd.DataFrame(dict_sales) df.head(5) # save it as csv file 5 df.to_csv('salesbyCars.csv) 3 4 Task 2 1 def getBest SellingCar(i_data_file, i_car): .. 2 3 ... 4 1 2 car - input ("Enter the car manufacturer name for which you want to find the best selling: 1 def bestsellingByCarManufacturer(i_data_file, i_car_list): ... 2 3 . 1 2 best selling_all_cars - print(best_selling_all_cars)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
