Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pickle or txt
#2
Pickle can store several objects by successive calls to dump. These objects can be retrieved one by one, which solves your memory issue.
from pathlib import Path
import pickle

this_dir = Path(__file__).parent

idata = ['some spam', '4 slices of ham', '12 eggs']

filename = this_dir / 'idata.pkl'

# pickle a sequence of objects one by one
with filename.open('wb') as ofh:
    pkl = pickle.Pickler(ofh)
    for x in idata:
        pkl.dump(x)

# unpickle a sequence of objects one by one
with filename.open('rb') as ifh:
    pkl = pickle.Unpickler(ifh)
    try:
        while True:
            x = pkl.load()
            print(x)
    except EOFError:
        pass
Output:
λ python paillasse/pf/pick.py some spam 4 slices of ham 12 eggs
Reply


Messages In This Thread
pickle or txt - by DPaul - Aug-22-2022, 09:23 AM
RE: pickle or txt - by Gribouillis - Aug-22-2022, 09:36 AM
RE: pickle or txt - by DPaul - Aug-22-2022, 10:54 AM
RE: pickle or txt - by DPaul - Aug-22-2022, 02:26 PM

Forum Jump:

User Panel Messages

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