Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pickle and unpickle

import  pickle
variety = ["sweet", "box", "cat"]
shape = ["back","spear", "log"]
pickleFile = open("pickle.txt", 'w')
pickle.dump(variety, pickleFile)
pickle.dump(shape, pickleFile)
pickleFile.close()

pickleFile = open("pickle.txt", 'r')
test = pickle.load(pickleFile)
shape = pickle.load(pickleFile)

print ("variety : ", test, " shape : ", shape)
pickleFile.close()

when I run above code I get the following error

line 6, in <module>
pickle.dump(variety, pickleFile)
TypeError: must be str, not bytes

and i am not sure if unpickling in variable 'test' will be possible or not cause I had pickled in with variable 'variety'

like image 201
dshri Avatar asked Oct 14 '15 14:10

dshri


People also ask

How do I Unpickle a pickle file?

As we said earlier, the load() method can be used to unpickle the pickled Python object. You have to first open the pickled file using rb (read-binary) permission and pass the opened file to the load() method, as shown below. The load() method unpickles the data and returns the actual object.

What is pickle and Unpickle?

“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy.

What is pickling and Unpickling with example?

Pickling: It is a process where a Python object hierarchy is converted into a byte stream. Unpickling: It is the inverse of Pickling process where a byte stream is converted into an object hierarchy.

How do you pickle in Python?

First, import pickle to use it, then we define an example dictionary, which is a Python object. Next, we open a file (note that we open to write bytes in Python 3+), then we use pickle. dump() to put the dict into opened file, then close. Use pickle.


2 Answers

According to help(pickle.dump),

The file argument must have a write() method that accepts a single bytes argument. It can thus be a file object opened for binary writing, a io.BytesIO instance, or any other custom object that meets this interface.

Looks like you have to open your file in binary mode. Don't forget to do the same for loading too.

import  pickle
variety = ["sweet", "box", "cat"]
shape = ["back","spear", "log"]
pickleFile = open("pickle.txt", 'wb')
pickle.dump(variety, pickleFile)
pickle.dump(shape, pickleFile)
pickleFile.close()

pickleFile = open("pickle.txt", 'rb')
test = pickle.load(pickleFile)
shape = pickle.load(pickleFile)

print ("variety : ", test, " shape : ", shape)
pickleFile.close()

Result:

variety :  ['sweet', 'box', 'cat']  shape :  ['back', 'spear', 'log']
like image 160
Kevin Avatar answered Oct 17 '22 03:10

Kevin


Your problem is that you are trying to write a pickled object into a text file. That is like trying to write a database in MS word.

The correct file extension for a pickled file is '.pkl'. The file must also be written to and read from in binary mode.

My suggestion would be to change the file extensions to '.pkl' and then use two with loops to neaten up your code. The corrections to the loading and writing is as simple as changing 'w' to 'wb' The result looks like this:

import  pickle
variety = ["sweet", "box", "cat"]
shape = ["back","spear", "log"]
with open("pickle.pkl", 'wb') as pickleFile:
    pickle.dump(variety, pickleFile)
    pickle.dump(shape, pickleFile)
    pickleFile.close()

with open("pickle.pkl", 'rb') as pickleFile:
    test = pickle.load(pickleFile)
    shape = pickle.load(pickleFile)

print ("variety : ", test, " shape : ", shape)
pickleFile.close()
like image 23
Liwa Avatar answered Oct 17 '22 03:10

Liwa