2017-02-20 2 views
0

아래의 코드를 실행하면 "EOFError : 입력이 끊어졌습니다"라는 오류 메시지가 표시됩니다. 무엇을 의미합니까 ?? 어떻게 수정 될 수 있습니까 ?? 화면에 레코드 정보를 출력하는 방법에 대해 설명합니다.EOFError : 입력 범위를 벗어난 경우

는 는
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

을 접수에 때 'while true '루프는 데이터의 끝에 도달했을 때 데이터에서 다른 프로세스를 수행하려고 시도하기 때문에 – WhatsThePoint

+0

직접'Car'리스트를 pickle/unpickle하려고 시도합니다. –

+0

어떻게 그럴 수 있습니까? – Chris

답변

1

당신이 당신에게 while 루프를 변경할 수 있습니다

break 입력의 끝에 당신의 while 루프 밖으로 코드가에 실패 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