Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use append with pickle in python?

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:

 import pickle
 p={}
 p[1]=2
 q={}
 q['a']=p
 p={}
 p[2]=0
 pickle.dump(q, open("save12.p","ab"))
 f={}
 f['b']=p
 pickle.dump(f,open("save12.p","ab"))

However, when I am loading the pickle file I don't find the value of dictionary f there?????

Can someone please suggest as to how should I go about appending in a pickle file???

Also databases like 'dbm' are not working for my need as i am working on windows

like image 607
Jannat Arora Avatar asked Oct 06 '12 17:10

Jannat Arora


People also ask

Can I append data to pickle file?

seek(0)– Pickle records can be concatenated into a file, so yes, you can just pickle.

Does pickle dump append or overwrite?

Usually, you'd open the file in append ( "ab" ) mode to add data at the end. However, Pickle doesn't support appending, so you'll have to save your data to a new file (come up with a different file name -- ask the user or use a command-line parameter such as -o test. txt ?)

How do you add a pickle in Python?

To use pickle, start by importing it in Python. To pickle this dictionary, you first need to specify the name of the file you will write it to, which is dogs in this case. Note that the file does not have an extension. To open the file for writing, simply use the open() function.

Can you edit a pickle file?

To delete a pickled object from a binary file you must rewrite the whole file. The pickle module doesn't deal with modifications at arbitrary portions of the stream, so there is no built-in way of doing what you want.


2 Answers

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 "<stdin>", line 1, in <module> EOFError 

so your unpickle code might look like

import pickle objs = [] while 1:     try:         objs.append(pickle.load(f))     except EOFError:         break 
like image 136
nneonneo Avatar answered Sep 30 '22 21:09

nneonneo


#To append to a pickle file import pickle  p={1:2} q={3:4} filename="picklefile" with open(filename, 'a+') as fp:     pickle.dump(p,fp)     pickle.dump(q,fp)   #To load from pickle file data = [] with open(filename, 'rb') as fr:     try:         while True:             data.append(pickle.load(fr))     except EOFError:         pass 
like image 38
Rangoli Thakur Avatar answered Sep 30 '22 23:09

Rangoli Thakur