Not able to load a pickle file. I am using python 3.5
import pickle
data=pickle.load(open("D:\\ud120-projects\\final_project\\final_project_dataset.pkl", "r"))
TypeError: a bytes-like object is required, not 'str'
. .
Also tried:
import pickle
data=pickle.load(open("D:\\ud120-projects\\final_project\\final_project_dataset.pkl", "rb"))
UnpicklingError: the STRING opcode argument must be quoted
. .
Same errors even when using with statements
import pickle
with open("D:\\ud120-projects\\final_project\\final_project_dataset.pkl", "rb") as f:
enron_data = pickle.load(f)
We can’t pickle local objects so that we are declaring that variable result as global. Here we have given only one print statement. If it is declared then we can say that the program has run successfully. In this program, we are going to see how to rectify the attribute error while multiprocessing.
" The pickle file has to be using Unix new lines otherwise at least Python 3.4's C pickle parser fails with exception: pickle.UnpicklingError: the STRING opcode argument must be quoted I think that some git versions may be changing the Unix new lines (' ') to DOS lines (' ').
1 with open ("/tmp/train.p", mode='rb') as training_data: ----> 2 train = pickle.load (training_data) UnpicklingError: invalid load key, '<'. I solved it by compressing the file, upload it and then unzip on the session.
But while dealing with chrome drivers, you may face can’t pickle errors depending on your function scope. When using the flask application in production mode, there are different instances of workers handling the traffic. In such cases, the chrome driver will not be saved from the custom function if it’s called by a different worker.
I'm using windows 10 and vscode, you should go to the final_project_dataset.pkl file and then change option CRLF to LF and then save the file then UnpicklingError: the STRING opcode argument must be quoted
error will be disappeared.
change CRLF to LF
then save the final_project_dataset.pkl file.
You definitely need the "rb" to read the file, which solves the first problem.
The second issue (STRING opcode argument) is because the file doesn't have Unix line endings. You need to run the pkl file through a script to convert them. If you see this thread, there is a script called "dos2unix" that will solve that for you:
How to convert DOS/Windows newline (CRLF) to Unix newline (\n) in a Bash script?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With