为什么不添加二元泡菜?

bra*_*urf 3 python pickle

我知道这并不是打算如何使用pickle模块,但我认为这会起作用.我正在使用Python 3.1.2

这是后台代码:

import pickle

FILEPATH='/tmp/tempfile'

class HistoryFile():
    """
    Persistent store of a history file  
    Each line should be a separate Python object
    Usually, pickle is used to make a file for each object,
        but here, I'm trying to use the append mode of writing a file to store a sequence
    """

    def validate(self, obj):
        """
        Returns whether or not obj is the right Pythonic object
        """
        return True

    def add(self, obj):
        if self.validate(obj):
            with open(FILEPATH, mode='ba') as f:    # appending, not writing
                f.write(pickle.dumps(obj))
        else:
            raise "Did not validate"

    def unpack(self):
        """
        Go through each line in the file and put each python object
        into a list, which is returned
        """
        lst = []
        with open(FILEPATH, mode='br') as f:
            # problem must be here, does it not step through the file?
            for l in f:
                lst.append(pickle.loads(l))
        return lst
Run Code Online (Sandbox Code Playgroud)

现在,当我运行它时,它只打印出传递给该类的第一个对象.

if __name__ == '__main__':

    L = HistoryFile()
    L.add('a')
    L.add('dfsdfs')
    L.add(['dfdkfjdf', 'errree', 'cvcvcxvx'])

    print(L.unpack())       # only prints the first item, 'a'!
Run Code Online (Sandbox Code Playgroud)

这是因为它看到了早期的EOF吗?也许追加仅适用于ascii?(在这种情况下,为什么让我做模式='ba'?)有没有更简单的方法来做到这一点?

Ale*_*lli 6

为什么你认为附加二元泡菜会产生一个泡菜?!Pickling让你可以一个接一个地放入(并取回)几个项目,所以显然它必须是一个"自我终止"的序列化格式.忘记线条,让它们回来!例如:

>>> import pickle
>>> import cStringIO
>>> s = cStringIO.StringIO()
>>> pickle.dump(23, s)
>>> pickle.dump(45, s)
>>> s.seek(0)
>>> pickle.load(s)
23
>>> pickle.load(s)
45
>>> pickle.load(s)
Traceback (most recent call last):
   ...
EOFError
>>> 
Run Code Online (Sandbox Code Playgroud)

只是抓住EOFError你告诉你什么时候你完成unpickling.