Wednesday 15 October 2014

pickle

After writing and loading JSON files several times to record and read a object of tree structure, I found it quite difficult to minimize the size of the JSON files because some floating point number in the object is printed to character and take up more memory space.  Google for few seconds, I found a solution to save files more efficiently. Surprisingly it is easy solution from socksolution.com. Pickle is going to save my file and my sleeping time as well. Here is the solution.

To record the object to file:

import pickle
pickleFile = open('root.pickle', 'wb')
pickle.dump(root, pickleFile, pickle.HIGHEST_PROTOCOL)
pickleFile.close()



To read the file to the object:

pickleFile = open('root.pickle', 'rb')
root = pickle.load(pickleFile)

pickleFile.close()


Tadaa!

No comments:

Post a Comment