Reputation: 11
I created a tree structure using python. Assume the tree has root node, left and right child. I need to acceess the tree whenever required. But on opening the file and running makes recreation of tree. As i have huge number of nodes in tree, so the method of recreation is time consuming. Please provide me a solution such that i can store the tree, fetch and update(insert or delete a node) whenever required.
Upvotes: 1
Views: 1013
Reputation: 11
you could use the pickle module that can be used to serialize and deserialize python objects quite easily.
import pickle
tree = {'a': 'b'}
# serializing the data
with open('/path/to/some/file', 'wb') as f:
pickle.dump(tree, f, pickle.HIGHEST_PROTOCOL)
# and deserializing it again
with open('/path/to/same/file', 'rb') as f:
tree = pickle.load(f)
Upvotes: 1