Skip to content Skip to sidebar Skip to footer

Value Error When Unpickling A File

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 unsuccessf

Solution 1:

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, so x, y = pickle.load(...) fails because it's trying to unpack the dictionary's keys to the identifiers x and y.

Instead, you should open the file once, and load from it twice:

with open("...roman.pk") as file_:
    first_dict = pickle.load(file_)  # file pointer is now at end of first object
    second_dict = pickle.load(file_)  # read in second object

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) and pickle that single object; this is much easier than relying on knowing exactly how many pickled objects are in the file.


Solution 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


Post a Comment for "Value Error When Unpickling A File"