我有下面的Python代码:
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()
这是cars.txt的内容
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
谁能帮我怎么解决这个问题?
这演示了如何通过使用适当的标准模块轻松处理问题中所示的数据。
这里没有显示对3个输出文件的处理,因为OP在这方面的意图不明确。
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')
输出:
{'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'}