2

I have a file with thousands of pickle objects. I want to access the nth pickled object without having to load all of the preceding objects. I checked the answers here and here but they don't answer my question. My understanding is that recursive unpickling starts at the top and moves the cursor to the next item after each item is unpickled.

Is it possible to manually specify where the cursor should start so I only unpickle the objects I want?

e.g.

import pickle

with open('file.pkl', 'rb') as f:
    for _ in range(2000, 2005):
        data = pickle.load(f) # This only loads the first 5 items, not the 2000-2005th items
berkelem
  • 2,005
  • 3
  • 18
  • 36

1 Answers1

1

In a new project, use the "shelve" module instead. You can access the pickled objects dictionary-like:

import shelve

sh= shelve.open("my_objects.db")
ll= [1,2,3,"Hello"]
sh["my_list"]= ll
sh["my_dict"]= { k:k*10 for k in range(10) }
sh.close()

sh= shelve.open("my_objects.db")
lizy= sh["my_list"]
dizy= sh["my_dict"]
sh.close()
kantal
  • 2,331
  • 2
  • 8
  • 15