Jan-05-2018, 09:35 PM
(This post was last modified: Jan-05-2018, 09:35 PM by karaokelove.)
(Jan-05-2018, 09:27 PM)Gribouillis Wrote: Here is a basic example of writing such a pickle file. Use it once withWRITE = True
to create the filefoo.pkl
, then use it as many times as you need withWRITE = False
to use the data.
from collections import namedtuple import pickle Record = namedtuple('Record', "id foo bar baz") WRITE = False if WRITE: L = [Record(i, "a"*100, "b" * 50, "c" * 50) for i in range(30000)] print('Writing') with open('foo.pkl', 'wb') as ofh: pickle.dump(L, ofh) else: print('Reading') with open('foo.pkl', 'rb') as ifh: L = pickle.load(ifh) print(len(L)) print(L[17865])
Wow, thanks for this! I'm about to start working on it and this will be a huge help. This will be my first functional Python program, so I'm sure I'll be posting a billion more questions over the next few hours...
P.S. I'm totally new on these forums. Is there a good way to reply or tag someone without quoting their entire message?