Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pickle problem
#11
Question: suppose that I write X number of objects (in casu different lists) to the pickle file,
like the example, but stretched out in time.
And I know in which order.
Can I now approach the pickle file and say "Give me the 7th object" ?
That would be very handy Smile
But I guess I have to start from the top and skip 1 though 6.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#12
(Sep-27-2022, 02:38 PM)DPaul Wrote: Can I now approach the pickle file and say "Give me the 7th object" ?
I don't think you can do that. Pickle does not provide random access. The closest object that I can think of is the persistent.list.PersistentList class available in the zodb pakckage. It stores a persistent list of objects in pickle format and provides random access, but using zodb might be overkill for what you want to do. Another alternative is to use a shelve which is a persistent dictionary storing pickled objects, provided by the python standard library. I have not used shelve in the last 20 years and I found that it was slow the last time I did it.
Reply
#13
Files are really not "random access" type storage. If all your pickle objects were some fixed size you could do what you want using file.seek() to move the file pointer to the start of the desired pickle object. But a fixed size for pickle objects would mean each item in the file would need to be padded to some large size that allowed for storing the largest allowable pickled object.

For variable sized objects you need some sort of index->offset table, and things are beginning to look like a file system, or a database.
Reply
#14
No random access, too bad.
Thanks for all the help. I have a procedure,
that leads to a pickle file the way I want it.
KISS is now the message for me.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 378 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  TypeError: cannot pickle n00sferatu 1 2,648 Dec-14-2021, 03:52 PM
Last Post: yakkaligiri
  Multiprocessing Can't pickle local object law 1 15,978 Aug-30-2021, 02:49 PM
Last Post: law
  Save/Loading using pickle Scordomaniac 4 3,029 Nov-24-2020, 06:11 PM
Last Post: Scordomaniac
  computing entropy using pickle files baran01 2 2,424 Dec-30-2019, 09:45 PM
Last Post: micseydel
  Tkinter don't get ver from file via pickle storzo 2 2,559 Jul-31-2019, 03:50 PM
Last Post: storzo
  pickle docs say bytes in one place, strings in another Skaperen 2 2,143 Jul-29-2019, 05:13 PM
Last Post: Skaperen
  pickle error SheeppOSU 4 10,953 Apr-20-2019, 04:50 PM
Last Post: SheeppOSU
  Using pickle.dump Friend 1 2,942 Feb-15-2019, 04:39 PM
Last Post: metulburr
  I'm having trouble with an OOP version of Pickle functionality CodeWolf 2 2,354 Dec-19-2018, 05:41 PM
Last Post: CodeWolf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020