python shelve

shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式

1.shelve

#!/usr/bin/env python
#-*- coding:utf-8 �*-

import shelve
d = shelve.open("shelve_test")

l1=["a1"]
l2=["a2"]
l3=["a3"]

d["k1"] = l1
d["k2"] = l2
d["k3"] = l3

d.close()

>>> import shelve
>>> a = shelve.open("shelve_test")
>>> a.get("k1")
['a1']
>>> a.get("k2")
['a2']
>>> a.get("k3")
['a3']
>>> a.get("k1")
['a1']


2.pick

import pickle

l1=["a1"]
l2=["a2"]
l3=["a3"]

with open("pickle","wb") as f:
    pickle.dump(l1,f)
    pickle.dump(l2, f)
    pickle.dump(l3, f)

>>> import pickle
>>> f = open("pickle","rb")
>>> pickle.load(f)    #只能按顺序读取
['a1']
>>> pickle.load(f)
['a2']
>>> pickle.load(f)
['a3']
>>> pickle.load(f)


你可能感兴趣的:(python,shelve)