Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all pickled data

Tags:

python

pickle

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

like image 800
oleg.foreigner Avatar asked May 01 '26 14:05

oleg.foreigner


1 Answers

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
like image 154
msvalkon Avatar answered May 03 '26 05:05

msvalkon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!