若有幫助到你,記得點贊o。
import shelve
s=shelve.open('test.dat')
#open函數以文件名爲參數,它會返回一個shelf對象,用來儲存數據。
#它的工作方式有點像字典。但鍵一定要是字符串,將內容儲存到磁盤中。
s['x']=['a','b','c']
s['x'].append('d')
print(s['x'])
#輸出:['a', 'b', 'c']
#上述操作並沒有達到預期效果,這是因爲操作不正確。
#使用shelve模塊修改儲存的對象,必須將臨時變量綁定到獲得的副本上,
#並且在它被修改後重新存儲到這個副本。
temp=s['x']
temp.append('d')
s['x']=temp
print(s['x'])
#輸出:['a', 'b', 'c', 'd']
s.close()