2017-02-20 5 views
0

以下のコードを実行すると、「EOFError:入力がなくなる」というエラーメッセージが表示されます 何を意味していますか?どのように修正することができますか?レコードの詳細を画面に出力する方法について説明します。EOFError:入力が無効になりました

あなたがこれまであなた whileループを変更することができます
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() 
+0

をrecievesます'while true 'ループは、データの終わりに達したときに、それがないデータに対して別のプロセスを実行しようとしているためです – WhatsThePoint

+0

' Car'リストを直接pickle/unpickleしようとします –

+0

それを行う方法? – Chris

答えて

1

これは、入力の最後にあなたのwhileループのうちbreakそれはコードがで失敗しEOFError

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() 
+0

ファイルの内容を画面に表示するには – Chris

+0

'json'モジュールは、出力する場合にはおそらくもっと使いやすいでしょう – WhatsThePoint

関連する問題