Python常用模块-shelve持久化

Python常用模块-shelve持久化

https://www.jb51.net/article/118198.htm

 

shelve模块是一个简单的key,value将内存数据通过文件持久化的模块。

shelve模块可以持久化任何pickle可支持的python数据格式。

shelve就是pickle模块的一个封装。

shelve模块是可以多次dump和load。

保存对象至shelve文件中

import shelve
import datetime

d = shelve.open('shelve.db')

list = ['x','y','z']
dic = {'a':'1','b':'2','c':'3'}
t = datetime.datetime.now()



d['list'] = list
d['dic'] = dic
d['t'] = t

d.close()

产生 'shelve.txt.db文件

að(ðn}ððððððtbtðcdatetime
datetime
qC
ð#5ðqðqRq.tð}q(XaqX1qXbqX2qXcqX3qu.dicð)q(X

读取

import shelve
f = shelve.open('shelve.txt','c')
print(f['list'])
print(f['dic'])
print(f['t'])
print('---------')
print(f['t'])

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day5/模块/a.py
['x', 'y', 'z']
{'a': '1', 'b': '2', 'c': '3'}
2018-07-04 15:35:53.065683
---------
2018-07-04 15:35:53.065683  #反复读取

进程已结束,退出代码0

遍历

import shelve
f = shelve.open('shelve.txt','c')
for item in f.items():
    print(item)
f.close()

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day5/模块/a.py
('list', ['x', 'y', 'z'])
('dic', {'a': '1', 'b': '2', 'c': '3'})
('t', datetime.datetime(2018, 7, 4, 15, 35, 53, 65683))

进程已结束,退出代码0

增加

import shelve
f = shelve.open('shelve.txt','c')
for item in f.items():
     print(item)
print('--------------------------')
f['c'] = {'ssssssssssssss'}
f['d'] = ['ddddddddd']
for item in f.items():
     print(item)
f.close()


/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day5/模块/a.py
('list', ['x', 'y', 'z'])
('t', datetime.datetime(2018, 7, 4, 15, 35, 53, 65683))
--------------------------
('list', ['x', 'y', 'z'])
('t', datetime.datetime(2018, 7, 4, 15, 35, 53, 65683))
('d', ['ddddddddd'])
('c', {'ssssssssssssss'})

进程已结束,退出代码0

修改

import shelve
f = shelve.open('shelve.txt','c')
for item in f.items():
     print(item)
print('--------------------------')

f['c'] = ['123']
for item in f.items():
     print(item)
f.close()


/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day5/模块/a.py
('list', ['x', 'y', 'z'])
('t', datetime.datetime(2018, 7, 4, 15, 35, 53, 65683))
('d', ['ddddddddd'])
('c', {'ssssssssssssss'})
--------------------------
('list', ['x', 'y', 'z'])
('t', datetime.datetime(2018, 7, 4, 15, 35, 53, 65683))
('d', ['ddddddddd'])
('c', ['123'])

进程已结束,退出代码0

删除

import shelve
f = shelve.open('shelve.txt','c')
for item in f.items():
     print(item)
del f['dic']
f.close()
f = shelve.open('shelve.txt','c')
for item in f.items():
     print(item)
f.close()

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day5/模块/a.py
('list', ['x', 'y', 'z'])
('dic', {'a': '1', 'b': '2', 'c': '3'})
('t', datetime.datetime(2018, 7, 4, 15, 35, 53, 65683))
('list', ['x', 'y', 'z'])
('t', datetime.datetime(2018, 7, 4, 15, 35, 53, 65683))

进程已结束,退出代码0

删除所有

import shelve
f = shelve.open('shelve.txt','c')
for k,v in f.items():
     print(k)
     del f[k]
f.close()

 

posted on 2018-07-04 16:08  光阴8023  阅读(290)  评论(0编辑  收藏  举报