+ 1
What is pickling and unpickling
5 Answers
+ 3
Rik Wittkopp
import pickle
example_dict = {1:"6",2:"2",3:"f"}
pickle_out = open("dict.pickle","wb")
pickle.dump(example_dict, pickle_out)
pickle_out.close()
First, import pickle to use it, then we define an example dictionary, which is a Python object.
Next, we open a file (note that we open to write bytes in Python 3+), then we use pickle.dump() to put the dict into opened file, then close.
The above code will save the pickle file for us, now we need to cover how to access the pickled file:
pickle_in = open("dict.pickle","rb") example_dict = pickle.load(pickle_in)
Open the pickle file
Use pickle.load() to load it to a var.
That's all there is to it, now you can do things like:
print(example_dict) print(example_dict[3])
This shows that we've retained the dict data-type.
+ 3
Aasthaš®š³
Thanks heaps for your example and for taking the time to explain how it works.
I still don't fully understand, but you have provided excellent food for thought.
+ 2
I honestly have no clue.. curious to know the answer
+ 2
Pickling is a way to convert a python object (list, dict, etc.) into a character stream.Ā The idea is that this character stream contains all the information necessary to reconstruct the object in another python script
+ 2
Aasthaš®š³ Could you please post an example of this concept.