error in loading pickle

Viewed 14943

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)
4 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.

If an entire script puts you off, it really just deserves one line:

import pickle
with open("D:\\path\\to\\file.data", "rb") as f:
    lines = [line.rstrip("\r\n") for line in f.readlines()]
    data = pickle.loads("\n".join(lines))

The only Fix for me was(Answered by Monkshow92 in Github) :

" 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 ('\n') to DOS lines ('\r\n').

You may use this code to change "word_data.pkl" to "word_data_unix.pkl" and then use the new .pkl file on the script "nb_author_id.py": dos2unix.txt

#!/usr/bin/env python
"""
convert dos linefeeds (crlf) to unix (lf)
usage: dos2unix.py 
"""
original = "word_data.pkl"
destination = "word_data_unix.pkl"

content = ''
outsize = 0
with open(original, 'rb') as infile:
    content = infile.read()
with open(destination, 'wb') as output:
    for line in content.splitlines():
        outsize += len(line) + 1
        output.write(line + str.encode('\n'))

print("Done. Saved %s bytes." % (len(content)-outsize))

dos2unix.py addapted from: http://stackoverflow.com/a/19702943

Small Tweak that I have found is , Changing "r" mode to "rb" byte object mode. And finally converting all the .pkl files using above python script to convert from Dos to Unix !

Answer Link : https://github.com/udacity/ud120-projects/issues/46 Full Credit : Monkshow92

Related