1
我想醃出對象列表中的對象包含列表。當我打開醃製文件時,除了列表外,我可以看到我的對象中的任何數據。我在下面放置代碼,這樣更有意義。蟒蛇泡菜不保存整個對象
包含列表的對象。
class TestPickle:
testNumber = None
testList = []
def addNumber(self, value):
self.testNumber = value
def getNumber(self):
return self.testNumber
def addTestList(self, value):
self.testList.append(value)
def getTestList(self):
return self.testList
這個例子我創建上述對象的列表(我加入一個對象來保持簡潔)
testPKL = TestPickle()
testList = []
testPKL.addNumber(12)
testPKL.addTestList(1)
testPKL.addTestList(2)
testList.append(testPKL)
with open(os.path.join(os.path.curdir, 'test.pkl'), 'wb') as f:
pickle.dump(testList, f)
這裏是我的打開文件醃製,並試圖訪問的示例數據,我只能從上面檢索testNumber,testList返回一個空列表。
pklResult = None
with open(os.path.join(os.path.curdir, 'test.pkl'), 'rb') as f:
pklResult = pickle.load(f)
for result in pklResult:
print result.getNumber() # returns 12
print result.testNumber # returns 12
print result.getTestList() # returns []
print result.testList # returns []
我想我在這裏失去了一些明顯的東西,但我沒有任何運氣發現它。感謝您的任何指導。
謝謝,就是這樣。 – user1904898