我想知道如何将类对象存储在文件中以便以后检索它。例如)我有一个堆栈类,我需要存储该类的对象以便稍后检索它。我尝试了以下方法:
output_file=open('hello.txt','w')
output_file.write(x)
其中 x 是堆栈类的对象。整个堆栈程序运行良好,当我来到这个存储部分时,我得到一个错误:
Traceback (most recent call last):
File "class.py", line 32, in <module>
output_file.write(x)
TypeError: expected a character buffer object
我该如何纠正这个错误?最好的存储方式是什么?