0

I have a problem with loading .pickle files which contain objects of the class collections.defaultdict.

I'm aware, that there are many answers regarding this topic out there (e.g. here), however none of them worked out.

I want to read the files of someone I'm collaborating with and he has apparantely no issues in saving defaultdicts as .pickle files, as his codes looks like the following:

dict_ = defaultdict()
example_keys = ["a", "b", "c"]
for i, key_ in enumerate(example_keys):
    dict_[key_] = 2 * i
pickle.dump(dict_, open("dict_.pickle", "wb"))

And he seems to have no issues, when loading this very file in his code lateron.

However, when I try to load the file using

from collections import defaultdict
with open("dict_.pickle", "rb") as handle:
     dict_ = pickle.load(handle)

I get Unable to get repr for <class 'collections.defaultdict'>.

Is there a way to fix this for me? Or should I ask my collaborator to switch to "normal" python dicts?

  • Cannot reproduce. Is there some detail involving how the original file was created or how you are trying to load it that might have been overlooked? – chepner Nov 07 '22 at 14:18
  • I have edited the part where I just wrote "do stuff to fill the dict" before, maybe that helps when trying to reproduce. In reality the list of keys looks different and the function to compute the values is also a little different, but that shouldn't matter here, I guess. – student7481 Nov 07 '22 at 14:53
  • Are you both using the same versions of Python and/or the same `pickle` protocol? – chepner Nov 07 '22 at 15:01

0 Answers0