我正在编写一个程序来打开一个用pickle 保存的数据库文件。但是如果我想使用 StringIO/cStringIO 将文件中的列表加载到内存中,它会说:
Opening database...
Loading database into memory...
Traceback (most recent call last):
...
File "C:\myfile.py", line 17 in open_database
database.write(databasefile)
TypeError: must be string or read-only character buffer, not list
这是我的代码:
def open_database(self):
print("Opening database...")
databasefile = open('database.dat', "r")
databasecontent = cPickle.load(databasefile)
databasefile.close()
print('Loading database into memory...')
database = cStringIO.StringIO()
database.write(databasecontent)
atexit.register(close_database)