铁氧体耗尽

2024-10-01 11:23:17 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行下面的代码时,我得到一条错误消息“eoferor:run out of input” 这是什么意思??如何纠正??以及如何在屏幕上输出记录的详细信息。在

import pickle # this library is required to create binary files
class CarRecord:
    def __init__(self):
      self.VehicleID = " "          
      self.Registration = " "
      self.DateOfRegistration = " "
      self.EngineSize = 0           
      self.PurchasePrice = 0.00

ThisCar = CarRecord()
Car = [ThisCar for i in range(2)]#list of 2 car records

Car[0].VehicleID = "CD333"
Car[0].Registration = "17888"
Car[0].DateOfRegistration = "18/2/2017"
Car[0].EngineSize = 2500
Car[0].PurchasePrice = 22000.00

Car[1].VehicleID = "AB123"
Car[1].Registration = "16988"
Car[1].DateOfRegistration = "19/2/2017"
Car[1].EngineSize = 2500
Car[1].PurchasePrice = 20000.00

CarFile = open ('Cars.TXT', 'wb' ) #open file for binary write
for j in range (2): # loop for each array element
    pickle.dump (Car[j], CarFile) # write a whole record to the binary file
CarFile.close() # close file

CarFile = open ('Cars.TXT','rb') # open file for binary read
Car = [] # start with empty list
while True: #check for end of file
    Car.append(pickle.load(CarFile)) # append record from file to end of list
CarFile.close()

Tags: oftoselfforregistrationopencarpickle
1条回答
网友
1楼 · 发布于 2024-10-01 11:23:17

您可以将while循环更改为:

当它接收到EOFError时,它将在输入结束时break退出{}循环

while True: #check for end of file
    try:
        Car.append(pickle.load(CarFile)) # append record from file to end of list
    except EOFError:
        break
CarFile.close()

相关问题 更多 >