原文地址:
http://www.voidcn.com/article/p-fqtqpwxp-wo.html
写入文件代码:
>>> import sys, shelve
>>>
>>> file = shelve.open("D:\\test.dat")
>>>
>>> data={}
>>> data['key1']="123456"
>>> data['key2']="eeeeee"
>>> data['key3']="ABCDEF"
>>> dataKey = "mydata"
>>> file[dataKey]=data
>>> file.close()
>>>
可以看到在D盘生成了一个test.dat的文件
读取数据代码:
>>> import sys, shelve
>>>
>>> file = shelve.open("D:\\test.dat")
>>> dataKey = "mydata"
>>> print file[dataKey]
{'key3': 'ABCDEF', 'key2': 'eeeeee', 'key1': '123456'}
>>> file.close()