Want to save an array to a txt file, try pickle, use the following code
with open(self.filename, 'a') as file:
pickle.dump(list,file,0)
Report error TypeError: must be str, not bytes
The reason found online is: Python3 added a new parameter named encoding to the open
function, and the default value of this new parameter is utf-8
. In this way, when performing read and write operations on the file handle, the system requires the developer to pass in an instance containing Unicode characters instead of accepting an instance of bytes containing binary data. To change it to open in binary mode, it is to change to ab
However, when you open the file in binary, the saved list becomes binary. When you open the txt file, you can't see the original array at all.
with open(self.filename, 'ab') as file:
file.write(%a\n".encode() % (self.memory_pattern))