阅读文件时,python unpickling EOFError

时间:2015-07-12 12:40:11

标签: python pickle

我编写了一个基本存储类,它将列表中的数据保存到文件中,然后允许从文件中读回它以保持持久性。它正在处理它最初编写的系统(Windows 7)我将它移动到第二个系统(Windows 8),现在它似乎出现故障。

我收到的错误是:

Traceback (most recent call last):
File "D:\Code Vault\pydb.py", line 69, in <module>
print Users.ReadData()
File "D:\Code Vault\pydb.py", line 23, in ReadData
self.data = cPickle.load(self.Inputstream)
EOFError

该课程的完整代码如下

class table():

    #TODO Insert Key Feild to facilitate ID Search 

    def __init__(self,*args):
        self.tablename = args[0]
        self.data = []
        self.colnames = {}
        self.filename = str(self.tablename)+ ".p"

        for e in args[1:]:
            self.colnames.update({e:len(self.colnames)})

        self.Filestorage = open(self.filename, "wb" )
        self.Filestorage.close()

    def ReadData(self):

        self.Inputstream = open(self.filename, "rb")
        self.Inputstream.seek(0)
        self.data = cPickle.load(self.Inputstream)
        self.Inputstream.close()
        return self.data

    def AddData (self, *args):
        self.tempdata = []

        for e in args:
            self.tempdata.append(e)
        if len(args) == len(self.colnames):
            self.data.append(self.tempdata)
        else:
            return "Incorrect argument quantities"
        return self.tempdata


    def Commit(self):
        self.Outputstream = open(self.filename, "ab" )
        cPickle.dump(self.data, self.Outputstream)
        self.Outputstream.close()
        return self.ReadData()

    def Search(self,query):
        for e in self.ReadData():
            if query in e:
                return self.ReadData().index(e)
        else:
            return "Value " + str(query) + " does not exist"

    def Update(self,query, edit):
        for e in self.data:
            if query in e:
                e[e.index(query)] = edit
        return self.data

    def DumpTable(self):
        self.data = []
        self.Outputstream = open(self.filename+".p", "wb" )
        cPickle.dump(self.data, self.Outputstream)
        self.Outputstream.close()
        self.ReadData()

Users = table("Users","Username","Password")
Users.AddData("bob","123456")
# Users.Commit()
# print Users.data
# print Users.filename
# print Users.data
print Users.ReadData()

它可能是一个代码失明的例子,因为它以前工作过。任何想法将不胜感激

2 个答案:

答案 0 :(得分:0)

回答问题的修正代码。

import pickle

class table():

    #TODO Insert Key Feild to facilitate ID Search 

    def __init__(self,*args):
        self.tablename = args[0]
        self.data = []
        self.colnames = {}
        self.filename = str(self.tablename)+ ".p"

        for e in args[1:]:
            self.colnames.update({e:len(self.colnames)})

        try:                                      #creating this conditional prevented the contents being erased upon instantiating the class
            f = open(self.filename,"rb")
        except IOError:
            self.createfile = open(self.filename, "wb" )
            self.createfile.close()

    def ReadData(self):
        self.Inputstream = open(self.filename, "rb")
        self.data = pickle.load(self.Inputstream)
        self.Inputstream.close()
        return self.data

    def AddData (self, *args):
        self.tempdata = []

        for e in args:
            self.tempdata.append(e)
        if len(args) == len(self.colnames):
            self.data.append(self.tempdata)
        else:
            return "Incorrect argument quantities"
        return self.tempdata

    def Commit(self):
        self.Outputstream = open(self.filename, "wb" )
        pickle.dump(self.data, self.Outputstream)
        self.Outputstream.close()

    def createfile(self):
        self.createfile = open(self.filename, "wb" )
        self.createfile.close()

    def Search(self,query):
        for e in self.ReadData():
            if query in e:
                return self.ReadData().index(e)
        else:
            return "Value " + str(query) + " does not exist"

    def Update(self,query, edit):
        for e in self.data:
            if query in e:
                e[e.index(query)] = edit
        return self.data

    def DumpTable(self):
        self.data = []
        self.Outputstream = open(self.filename+".p", "wb" )
        pickle.dump(self.data, self.Outputstream)
        self.Outputstream.close()
        self.ReadData()

答案 1 :(得分:-1)

解决方案似乎在pickle.load() raising EOFError in Windows

即。需要打开文件,以便在“rb”标志的二进制模式下读取。