pickled list 1

pickled list

import pickle
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...What's,uh...What's wrong with it?", "I'll tell you what's wrong with it, my lad. 'E's dead, that's what's wrong with it!", "No, no, 'e's uh,...he's resting."]
with open('parrot.pkl', 'wb') as f:
   pickle.dump(mylist, f)
with open('parrot.pkl', 'rb') as f:
   mynewlist = pickle.load(f)
mynewlist
output: ['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...What's,uh...What's wrong with it?", "I'll tell you what's wrong with it, my lad. 'E's dead, that's what's wrong with it!", "No, no, 'e's uh,...he's resting."]

Here is what the above code is Doing:
1. We open the file parrot.pkl in write binary mode (‘wb’) and assign it to the variable f.
2. We then use pickle.dump() to dump the contents of mylist into f.
3. We then open the file parrot.pkl in read binary mode (‘rb’) and assign it to the variable f.
4. We then use pickle.load() to load the contents of f into mynewlist.
5. We then print mynewlist to the screen.

Similar Posts