Search code examples
pythonobjectpickleagent-based-modeling

Error when retrieving saved object using pickle


Working with the MESA agent based modelling package. Using pickle to save the state of my intermediate model. But when retrieving the saved model the execution ends up in error saying:

  File "/home/demonwolf/PycharmProjects/pythonProject1/main.py", line 281, in <module>
    empty_model = pickle.load(f)
  File "/home/demonwolf/anaconda3/envs/ABM/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte```



Any help would be appreciated.
Thanks in advance.

Solution

  • The file (the f parameter in pickle.load(f)) should be open in binary read (rb) mode, not the default text (r) mode.

    with open("path/to/your/pickle.bin", "rb") as f:
        empty_model = pickle.load(f)