+ 1

What is pickling and unpickling

19th Oct 2019, 6:25 AM
pooja
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.
22nd Oct 2019, 6:25 PM
AasthašŸ‡®šŸ‡³
AasthašŸ‡®šŸ‡³ - avatar
+ 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.
24th Oct 2019, 9:09 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
I honestly have no clue.. curious to know the answer
19th Oct 2019, 7:14 AM
Brave Tea
Brave Tea - avatar
+ 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
19th Oct 2019, 12:29 PM
AasthašŸ‡®šŸ‡³
AasthašŸ‡®šŸ‡³ - avatar
+ 2
AasthašŸ‡®šŸ‡³ Could you please post an example of this concept.
20th Oct 2019, 10:25 AM
Rik Wittkopp
Rik Wittkopp - avatar