【发布时间】:2018-12-17 11:44:46
【问题描述】:
我正在学习写入和读取二进制文件,并且正在从书中复制代码以说明这是如何完成的。我把书中的两段代码放在一起来完成这个任务。在编译我的代码时,我得到一个 EOF 错误,并且不确定是什么原因造成的。你能帮我吗?下面列出了我正在编写的代码。
class CarRecord: # declaring a class without other methods
def init (self): # constructor
self .VehicleID = ""
self.Registration = ""
self.DateOfRegistration = None
self.EngineSize = 0
self.PurchasePrice = 0.00
import pickle # this library is required to create binary f iles
ThisCar = CarRecord()
Car = [ThisCar for i in range (100)]
CarFile = open ('Cars.DAT', 'wb') # open file for binary write
for i in range (100) : # loop for each array element
pickle.dump (Car[i], CarFile) # write a whole record to the binary file
CarFile.close() # close file
CarFile = open( 'Cars.DAT','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 l i st
CarFile.close()
【问题讨论】:
-
您能否编辑您的问题以形成结构化代码
-
您在之前的相同问题中已经有了很好的答案。