1

I am trying to figure out how to add data in already saved data with pickle in Python

import pickle
load_written_things = open("written_things.pkl", "rb")
added_stuff = input("write:")
written_things = pickle.load(load_written_things)  +  pickle.dump(added_stuff, open("written_things.pkl", "wb"))                                                          
print (written_things)

i had previously created the written_things.pkl and i had written something in it, while i was writing code for sth else. Now i want to add things and an erron appears saying EOFError: Ran out of input. I have attempted many times to add stuff with different ways but i cant figure out a way, althouhg i searched for a solution on youtube and on stack overflow.

2 Answers2

0

You could try the following:

import pickle

load_written_things = open("written_things.pkl", "rb")
added_stuff = raw_input("write: ")
written_things = pickle.load(load_written_things)  +  added_stuff

pickle.dump(written_things, open("written_things.pkl", "wb"))                                                          
print (written_things)

Update for the 2nd question:

Well let's say that you have dumped an initial list in 'written_things.pkl' like so:

#!/usr/bin/env python

import pickle

# initial list dumped in 'written_things.pkl'
alist = ['name1', 'name2', 'name3']
with open('written_things.pkl', 'wb') as f: pickle.dump(alist,f)

Now if I understood correctly you want to update this list with new items and dump the new list/result in 'written_things.pkl' using pickle. A way to do that would be the following:

# old stuff
load_written_things = open("written_things.pkl", "rb")
old = pickle.load(load_written_things)

new_item = raw_input('>> New item: ')
with open("written_things.pkl", "w") as f:  
    pickle.dump(old+[new_item], f)

# to read back the data
with open("written_things.pkl", "rb") as f:
    data = pickle.load(f)

Also have in mind that in this way we add one item at a time. So if you want to enter multiple items at once, you could enter your items like this: item1,item2,item3 etc... (or any other separator) and then replace the line:

pickle.dump(old+[new_item], f) --with--> pickle.dump(old+new_item.split(','), f)

Also another stuff to consider as mentioned here, is that the pickle module is not secure against erroneous or maliciously constructed data. That means that you must be sure that the data in the .pkl file are not malicious. There are many techniques in the wild that allow arbitrary code execution with python's pickles.

As just an alternative to pickle you can always put your output (may be a list, dictionary etc...) in a file and then use ast.literal_eval to read back your data. As mentioned in the module itself and also here, ast.literal_eval is considered safe to use.

coder
  • 12,832
  • 5
  • 39
  • 53
  • Thanks for the answer. Just one question. How can i make each added thing a separated entry in a list or dictionary. I want to make a Gui in which names will be stored by the user and appear in label or list. So if there is any way to make each added data separated I would really like to know it. Thank you again for the answer and thank you if you answer this second question – Giannis Giannoulas Aug 24 '17 at 17:21
  • @GiannisGiannoulas I added a possible answer to your 2nd question ! – coder Aug 24 '17 at 18:13
  • Thank you i really appreciate your help. I was trying to understand for 6 hours how to do it. And finally i know the way. – Giannis Giannoulas Aug 25 '17 at 16:58
  • No prob, glad to help! – coder Aug 25 '17 at 17:03
0

Try the following.

import pickle

added_stuff = raw_input("write:")
pickle.dump(added_stuff, open("written_things.pkl","a"))                                                          

print added_stuff

Basically this appends your input into the written_things.pkl .

~ Using python 2.7.6

F. Leone
  • 594
  • 3
  • 14