Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error in loading pickle

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)
like image 215
Nimish Bansal Avatar asked Jul 28 '17 08:07

Nimish Bansal


People also ask

Why we can’t pickle local objects?

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.

What's wrong with pickle in Python?

" 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 (' ').

How to solve unpicklingerror-invalid Load Key?

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.

Can’t pickle errors while dealing with chrome drivers?

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.


2 Answers

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.

enter image description here

change CRLF to LF

enter image description here

then save the final_project_dataset.pkl file.

like image 127
ehsan maddahi Avatar answered Sep 30 '22 12:09

ehsan maddahi


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?

like image 30
Dale Avatar answered Sep 30 '22 12:09

Dale