Reputation:
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?
Upvotes: 3
Views: 1426
Reputation: 123473
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
Upvotes: 2