I have the below Python code:
carFile = open("cars.txt", "r")
file_data = carFile.read()
carFile.close()
car_details_name = file_data.split("\n")[0].split("-")
car_details = file_data.split("\n")[1:]
cars = []
no_of_cars = len(car_details)
for i in range(no_of_cars):
car = dict()
for j, detail in enumerate(car_details_name):
car[detail] = car_details[i].split("-")[j]
car["car_price"] = str(int(car["selling_price"]) - int(car["discount"]) + int(car["tax"]))
car["car_type"] = "LUXURY" if int(car["car_price"])>=8000 else "CONVENIENCE"
cars.append(car)
car_details_name.append('car_price')
car_details_name.append('car_type')
for i in range(no_of_cars):
print(cars[i])
print("\n")
processedCarFile = open("processedcars.txt", "w")
luxuryCarFile = open("luxurycars.txt", "w")
convenienceCarFile = open("conveniencecars.txt", "w")
processedCarFile.write(" - ".join(car_details_name) + "\n")
luxuryCarFile.write(" - ".join(car_details_name) + "\n")
convenienceCarFile.write(" - ".join(car_details_name) + "\n")
for car in cars:
processedCarFile.write(" - ".join(list(car.values())) + "\n")
if car["car_type"]=="LUXURY":
luxuryCarFile.write(" - ".join(list(car.values())) + "\n")
else:
convenienceCarFile.write(" - ".join(list(car.values())) + "\n")
processedCarFile.close()
luxuryCarFile.close()
convenienceCarFile.close()
and here is the cars.txt content
car_model_number-car_manufacturer-tax-selling_price-discount
15AB83FUFBFU8-Maruti Suzuki-1000-7000-500
54GDG8FHNJVFV-Tata-800-7200-800
FRF84R99NKFNF-Mahindra-1000-8000-1200
HFD748BF4FJBF-Hyundai-750-11000-1750
I got below error:
IndexError Traceback (most recent call last)
Cell In[1], line 17
15 car = dict()
16 for j, detail in enumerate(car_details_name):
---> 17 car[detail] = car_details[i].split("-")[j]
19 car["car_price"] = str(int(car["selling_price"]) - int(car["discount"]) + int(car["tax"]))
21 car["car_type"] = "LUXURY" if int(car["car_price"])>=8000 else "CONVENIENCE"
IndexError: list index out of range
can anyone help me how to fix this?
This demonstrates how, by using appropriate standard modules, it's easy to handle data such as that shown in the question.
The handling of the 3 output files is not shown here as OP's intent in that respect is unclear.
from csv import DictReader
FILENAME = 'cars.txt'
LUXURY_PRICE = 8_000
with open(FILENAME, newline='') as car_data:
carlist = []
for d in DictReader(car_data, delimiter='-'):
price = int(d['tax']) + int(d['selling_price']) - int(d['discount'])
d['car_price'] = price
d['car_type'] = 'LUXURY' if price >= LUXURY_PRICE else 'CONVENIENCE'
carlist.append(d)
print(*carlist, sep='\n')
Output:
{'car_model_number': '15AB83FUFBFU8', 'car_manufacturer': 'Maruti Suzuki', 'tax': '1000', 'selling_price': '7000', 'discount': '500', 'car_price': 7500, 'car_type': 'CONVENIENCE'}
{'car_model_number': '54GDG8FHNJVFV', 'car_manufacturer': 'Tata', 'tax': '800', 'selling_price': '7200', 'discount': '800', 'car_price': 7200, 'car_type': 'CONVENIENCE'}
{'car_model_number': 'FRF84R99NKFNF', 'car_manufacturer': 'Mahindra', 'tax': '1000', 'selling_price': '8000', 'discount': '1200', 'car_price': 7800, 'car_type': 'CONVENIENCE'}
{'car_model_number': 'HFD748BF4FJBF', 'car_manufacturer': 'Hyundai', 'tax': '750', 'selling_price': '11000', 'discount': '1750', 'car_price': 10000, 'car_type': 'LUXURY'}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.