How to use append with pickle in python?

前端 未结 2 1920
闹比i
闹比i 2020-11-28 10:52

i need to append to a pickle file (as i don\'t have the entire dictionary with me at one go). So for doing the same I have written the following code:

 impor         


        
2条回答
  •  被撕碎了的回忆
    2020-11-28 11:44

    Pickle streams are entirely self-contained, and so unpickling will unpickle one object at a time.

    Therefore, to unpickle multiple streams, you should repeatedly unpickle the file until you get an EOFError:

    >>> f=open('a.p', 'wb')
    >>> pickle.dump({1:2}, f)
    >>> pickle.dump({3:4}, f)
    >>> f.close()
    >>> 
    >>> f=open('a.p', 'rb')
    >>> pickle.load(f)
    {1: 2}
    >>> pickle.load(f)
    {3: 4}
    >>> pickle.load(f)
    Traceback (most recent call last):
      File "", line 1, in 
    EOFError
    

    so your unpickle code might look like

    import pickle
    objs = []
    while 1:
        try:
            objs.append(pickle.load(f))
        except EOFError:
            break
    

提交回复
热议问题