python shelve模块

将数据以文件的形式保存

import shelve

def create():
    try:
        db = shelve.open('db.dat', 'c')
        db['name'] = 'jam'
        db['home'] = 'ar'
    finally:
        db.close()

def load():
    db = shelve.open('db.dat', 'r')
    for i in db.items():
        print i
    db.close()


if __name__ == '__main__':
    create()
    load()

如果是做文件缓存,将内容保存为py,并使用execfile来读取,将会更加高效

原文地址:https://www.cnblogs.com/itfenqing/p/4429468.html