I have been given a file that contains two pickled objects- it was sent to me as a .pk file. I was told it had two objects, however, my attempt to unpikle these files is unsuccessful.
The first pickled object contains a dictionary of pairs of numbers with their roman numerals, and the second contains dictionary pairs of roman numerals with their numbers. How can I unpickle these two dictionaries separately?
This is what I have below, along with the error message I am receiving:
import pickle
x,y=pickle.load(open("C://Users//Documents//roman.pk", "rb"))
print(x,y)
Error Message:
ValueError: too many values to unpack (expected 2)
pickle.load should load the data structure into one parameter. if you have 2 dictionary in roman.pk, it depends on how the 2 dictionary are grouped
e.g (dict1, dict2)
In this case: You probably want to try: (x,y)= pickle.load(open("C://Users//Documents//roman.pk", "rb")) print len(x) print len(y) To check if it loads correctly
pickle.load
will only load the first pickled object that it finds in the file. In your case, that's a dictionary with more than two keys, sox, y = pickle.load(...)
fails because it's trying to unpack the dictionary's keys to the identifiersx
andy
.Instead, you should
open
the file once, andload
from it twice:Alternatively, encourage whoever is supplying you with the file to put the two dictionaries into a single object, e.g. a tuple
(first_dict, second_dict)
andpickle
that single object; this is much easier than relying on knowing exactly how many pickled objects are in the file.