转自:http://python.01314.cn/201506145.html
#coding=utf-8 import anydbm def CreateData(): try: db = anydbm.open('db.dat', 'c') # key与value必须是字符串 # db['int'] = 1 # db['float'] = 2.3 db['string'] = "I like python." db['key'] = 'value' finally: db.close() def LoadData(): db = anydbm.open('db.dat', 'r') for item in db.items(): print item db.close() if __name__ == '__main__': CreateData() LoadData()
import shelve def CreateData(): try: db = shelve.open('db.dat', 'c') # key与value必须是字符串 db['int'] = 1 db['float'] = 2.3 db['string'] = "I like python." db['key'] = 'value' finally: db.close() def LoadData(): db = shelve.open('db.dat', 'r') for item in db.items(): print item db.close() if __name__ == '__main__': CreateData() LoadData()