I have a pickled file with some data structures. I don't know the exact amount and types of elements. How to get all objects into a dict or a list?
The question is how to iterate through the file not knowing the number of entries?
Do all objects are stored as strings?
EDIT: I'm using such code to save data in file:
import pickle
def _save(_file, *_obj):
with open(_file, 'w') as f:
for obj in _obj:
pickle.dump(obj, f)
the only solution I see right now is to store the number of objects as a first entry. read it, then read everything else.
I can easily unpickle data that way:
list_data = [1, 2, 3, 4]
dict_data = {1:'a', 2:'b'}
tuple_data = (1, 2, 3)
_save('my_pickle.pckl', list_data, dict_data, tuple_data)
with open('my_pickle.pckl', 'r') as f:
item1 = pickle.load(f)
print item1
item2 = pickle.load(f)
print item2
item3 = pickle.load(f)
print item3
this gives me what I want... but I need to do it in a loop
You could add all your objects to a list and then do whatever you prefer with them.
with open(pickle_file) as f:
unpickled = []
while True:
try:
unpickled.append(pickle.load(f))
except EOFError:
break
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With