3

I want to store a Python object that contains several numpy arrays in a file. I found pickle but I'm always getting an UnicodeDecodeError when loading a stored object:

  Traceback (most recent call last):
File "system.py", line 46, in <module>
  m2 = System.loadMemory('m1.pickle')
 File "system.py", line 28, in loadMemory
  memory = pickle.load(filehandler)
  File "/home/daniel-u1/anaconda3/lib/python3.5/codecs.py", line 321, 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

Method saveMemory works fine, but loadMemory throws the error at pickle.load:

@staticmethod
def saveMemory(m1, filename):
    filehandler = open(filename, 'wb')
    pickle.dump(m1, filehandler)

@staticmethod
def loadMemory(filename):
    filehandler = open(filename, 'r')
    memory = pickle.load(filehandler)
    return memory

Has anyone an idea on how to solve this issue?

3
  • 2
    Why are you writing the file in binary mode and reading in text mode? Commented Feb 15, 2017 at 17:42
  • 2
    In other words, use open(filename, 'rb') to open the file in loadMemory(). Commented Feb 15, 2017 at 17:54
  • 1
    @martineau Thank you very much, that was the thing. Could you please answer in a separate post so I can mark this as solved? Commented Feb 15, 2017 at 18:23

1 Answer 1

2

The problem is you wrote the file binary mode ('wb') but then try to read it back in as text mode ('r'). So to fix it all you should need to do is change one line:

@staticmethod
    def loadMemory(filename):
        filehandler = open(filename, 'rb')  # must read in binary mode, too
        memory = pickle.load(filehandler)
        return memory
Sign up to request clarification or add additional context in comments.

Comments