python – Dump a list in a pickle file and retrieve it back later
python – Dump a list in a pickle file and retrieve it back later
Pickling will serialize your list (convert it, and its entries to a unique byte string), so you can save it to disk. You can also use pickle to retrieve your original list, loading from the saved file.
So, first build a list, then use pickle.dump
to send it to a file…
Python 3.4.1 (default, May 21 2014, 12:39:51)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type help, copyright, credits or license for more information.
>>> mylist = [I wish to complain about this parrot what I purchased not half an hour ago from this very boutique., Oh yes, the, uh, the Norwegian Blue...Whats,uh...Whats wrong with it?, Ill tell you whats wrong with it, my lad. Es dead, thats whats wrong with it!, No, no, es uh,...hes resting.]
>>>
>>> import pickle
>>>
>>> with open(parrot.pkl, wb) as f:
... pickle.dump(mylist, f)
...
>>>
Then quit and come back later… and open with pickle.load
…
Python 3.4.1 (default, May 21 2014, 12:39:51)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type help, copyright, credits or license for more information.
>>> import pickle
>>> with open(parrot.pkl, rb) as f:
... mynewlist = pickle.load(f)
...
>>> mynewlist
[I wish to complain about this parrot what I purchased not half an hour ago from this very boutique., Oh yes, the, uh, the Norwegian Blue...Whats,uh...Whats wrong with it?, Ill tell you whats wrong with it, my lad. Es dead, thats whats wrong with it!, No, no, es uh,...hes resting.]
>>>