一、定义

Shelve是对象持久化保存方法,将对象保存到文件里面,缺省(即默认)的数据存储文件是二进制的。

二、用途

可以作为一个简单的数据存储方案。

三、用法

使用时,只需要使用open函数获取一个shelf对象,然后对数据进行增删改查操作,在完成工作、并且将内存存储到磁盘中,最后调用close函数变回将数据写入文件。

四、关联模块Anydbm

相同点:
1.anydbm, shelve 都是对象持久化保存方法,将对象保存到文件里面,缺省的数据存储文件是二进制的。这两个模块允许我们将一个磁盘上的文件与一个”dict-like”对象(类字典对象)关联起来,操作这个“dict-like”对象,就像操作dict对象一项,最后可以将“dict-like”的数据持久化到文件。
2.都可以使用open函数。 区别:
anydbm的key和value的类型必须都是字符串,而shelve的key要求必须是字符串,value则可以是任意合法的python数据类型。

五、方法

1.shelve.open(filename, flag=’c’, protocol=None, writeback=False):创建或打开一个shelve对象。shelve默认打开方式支持同时读写操作。
filename是关联的文件路径。
可选参数flag,默认为‘c’,如果数据文件不存在,就创建,允许读写;可以是: ‘r’: 只读;’w’: 可读写; ‘n’: 每次调用open()都重新创建一个空的文件,可读写。
protocol:是序列化模式,默认值为None。具体还没有尝试过,从pickle的资料中查到以下信息【protocol的值可以是1或2,表示以二进制的形式序列化】 2.shelve.close()
同步并关闭shelve对象。
注意:每次使用完毕,都必须确保shelve对象被安全关闭。同样可以使用with语句
with shelve.open('spam') as db:
db['eggs'] = 'eggs'

六、writeback参数

writeback:默认为False。当设置为True以后,shelf将会将所有从DB中读取的对象存放到一个内存缓存。当我们close()打开的shelf的时候,缓存中所有的对象会被重新写入DB。
writeback方式有优点也有缺点。
优点是减少了我们出错的概率,并且让对象的持久化对用户更加的透明了;但这种方式并不是所有的情况下都需要,首先,使用writeback以后,shelf在open()的时候会增加额外的内存消耗,并且当DB在close()的时候会将缓存中的每一个对象都写入到DB,这也会带来额外的等待时间。因为shelve没有办法知道缓存中哪些对象修改了,哪些对象没有修改,因此所有的对象都会被写入。 注意:为了保存增、删、改的内容,建议显示的标明writeback=True。
# 七、代码示例
# 1.创建一个shelf对象,直接使用open函数即可 import shelve
s = shelve.open('test_shelf.db') #
try:
s['kk'] = {'int': 10, 'float': 9.5, 'String': 'Sample data'}
s['MM'] = [1, 2, 3]
finally:
s.close() # 2.如果想要再次访问这个shelf,只需要再次shelve.open()就可以了,然后我们可以像使用字典一样来使用这个shelf import shelve
try:
s = shelve.open('test_shelf.db')
value = s['kk']
print(value)
finally:
s.close() # 3.对shelf对象,增、删、改操作 import shelve
s = shelve.open('test_shelf.db', flag='w', writeback=True)
try:
# 增加
s['QQQ'] = 2333
# 删除
del s['MM']
# 修改
s['kk'] = {'String': 'day day up'}
finally:
s.close() # 注意:flag设置为‘r’-只读模式,当程序试图去修改一个以只读方式打开的DB时,将会抛一个访问错误的异常。异常的具体类型取决于anydbm这个模块在创建DB时所选用的DB。异常举例:anydbm.error: need ‘c’ or ‘n’ flag to open new db # 4.循环遍历shelf对象 import shelve
s = shelve.open('test_shelf.db')
try:
# 方法一:
for item in s.items():
print ('键[{}] = 值[{}]'.format(item[0], s[item[0]]))
# 方法二:
for key, value in s.items():
print(key, value)
finally:
s.close() # 5.备注一个错误:
# open中的参数filename,起初认为需要手动新建一个.db,或者.dat的文件,目前电脑中无任何真正的数据库文件,所以采用了新建txt文件,修改后缀的方法创建.db,或者.dat的文件。
# 解释器报错,提示内容为:"anydbm.error: db type could not be determined",
# 原因是是filename已经存在,并且格式与shelve不符,所以提示 “db type could not be determined”。
# 解决方法是,删除该文件。首次运行后会自动生成该filename文件。
# 6.稍微复杂些的案例,实现一个简单提问式的数据库 # encoding:utf-8
# 2018/3/8 # 简单的数据库 import sys,shelve def print_help():
'存储(增加)、查找、更新(修改)、循环打印、删除、退出、帮助'
print('The available commons are: ')
print('store : Stores information about a person')
print('lookup : Looks up a person from ID numbers')
print("update : Update a person's information from ID number")
print('print_all: Print all informations')
print("delete : Delete a person's information from ID number")
print('quit : Save changes and exit')
print('? : Print this message') def store_people(db):
pid = input('Please enter a unique ID number: ')
person = {}
person['name'] = input('Please enter the name: ')
person['age'] = input('Please enter the age: ')
person['phone'] = input('Please enter the phone: ')
db[pid] = person
print("Store information: pid is %s, information is %s" % (pid, person)) def lookup_people(db):
pid = input('Please enter the number: ')
field = input('What would you like to know? (name, age, phone) ')
if pid in db.keys():
value = db[pid][field]
print("Pid %s's %s is %s" % (pid, field, value))
else:
print('Not found this number') def update_people(db):
pid = input('Please enter the number: ')
field = input('What would you like to update? (name, age, phone) ')
newvalue = input('Enter the new information: ')
if pid in db.keys():
value = db[pid]
value[field] = newvalue
print("Pid %s's %s update information is %s" % (pid, field, newvalue))
else:
print("Not found this number, can't update") def delete_people(db):
pid = input('Please enter the number: ')
if pid in db.keys():
del db[pid]
print("pid %s's information delete done" % pid)
else:
print( "Not found this number, can't delete") def print_all_people(db):
print( 'All information are: ')
for key, value in db.items():
print(key, value) def enter_cmd():
cmd = input('Please enter the cmd(? for help): ')
cmd = cmd.strip().lower()
return cmd def main():
database = shelve.open('database201803.dat', writeback=True)
try:
while True:
cmd = enter_cmd()
if cmd == 'store':
store_people(database)
elif cmd == 'lookup':
lookup_people(database)
elif cmd == 'update':
update_people(database)
elif cmd == 'print_all':
print_all_people(database)
elif cmd == 'delete':
delete_people(database)
elif cmd == '?':
print_help()
elif cmd == 'quit':
return
finally:
database.close() if __name__ == '__main__':
main()
# shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;
# key必须为字符串,而值可以是python所支持的数据类型
# shelve模块(**)------可以当做数据库用,以后基本不会用,(可以很方面的往文件中写数据类型和读)
import shelve #存取很方便(可以做一个简单的数据存储方案)
f=shelve.open(r'sheve.txt')
f['stu1_info']={'name':'egon','age':18,'hobby':['piao','smoking','drinking']} #存
f['stu2_info']={'name':'gangdan','age':53}
f['school_info']={'website':'http://www.pypy.org','city':'beijing'}
print(f['stu1_info']['hobby'])
f.close() import shelve
d=shelve.open(r'a.txt') #生成三个文件分别是:a.txt.bak\a.txt.dat\a.txt.dir
d['tom']={'age':18,'sex':'male'} #存的时候会生成三个文件,不用管,是python的一种处理机制
print(d['tom']['sex']) #可以取出字典中的key对应的value
print(d['tom']) #取出tom对应的字典
d.close() import shelve
d=shelve.open(r'a.txt',writeback=True) #writeback=True,对子字典修改完后要写回,否则不会看到修改后的结果
d['egon']={'age':18,'sex':'male'} #存的时候会生成三个文件,不用管,是python的一种处理机制
d['egon']['age']=20 #将年龄修改为20
print(d['egon']['age']) #此时拿到的是修改后的年龄
print(d['egon']['sex'])
d.close()

python之shelve模块详解的更多相关文章

  1. python之OS模块详解

    python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...

  2. python之sys模块详解

    python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...

  3. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

  4. Python中time模块详解

    Python中time模块详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. ...

  5. Python的logging模块详解

          Python的logging模块详解 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.日志级别 日志级别指的是产生的日志的事件的严重程度. 设置一个级别后,严重程度 ...

  6. Python全栈之路----常用模块----序列化(json&pickle&shelve)模块详解

    把内存数据转成字符,叫序列化:把字符转成内存数据类型,叫反序列化. Json模块 Json模块提供了四个功能:序列化:dumps.dump:反序列化:loads.load. import json d ...

  7. python中常用模块详解二

    log模块的讲解 Python 使用logging模块记录日志涉及四个主要类,使用官方文档中的概括最为合适: logger提供了应用程序可以直接使用的接口API: handler将(logger创建的 ...

  8. python的re模块详解

    一.正则表达式的特殊字符介绍 正则表达式 ^ 匹配行首 $ 匹配行尾 . 任意单个字符 [] 匹配包含在中括号中的任意字符 [^] 匹配包含在中括号中的字符之外的字符 [-] 匹配指定范围的任意单个字 ...

  9. Python中time模块详解(转)

    在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: ...

随机推荐

  1. Flex Builder 4.6切换语言

    一.修改Flex builder 1.用无格式编辑器打开FlashBuilder.ini 2.把zh_CN替换成"en_US" 二.修改MyEclipse插件 1.用无格式编辑器打 ...

  2. phpstorm设置编码格式

    phpstorm设置编码格式 默认: utf-8格式 设置方法: file -> settings -> Editor -> file encodng -> project e ...

  3. FutureTask并发详解,通俗易懂

    最近做项目,使用到了FutureTask和主线程并发,应用到实际中还是挺实用的,特在此总结一下. 有不对之处,忘各位多多指出. package com.demo; import java.util.c ...

  4. 爬虫系列---scrapy全栈数据爬取框架(Crawlspider)

    一 简介 crawlspider 是Spider的一个子类,除了继承spider的功能特性外,还派生了自己更加强大的功能. LinkExtractors链接提取器,Rule规则解析器. 二 强大的链接 ...

  5. 一次CMS GC问题排查过程(理解原理+读懂GC日志)

    这个是之前处理过的一个线上问题,处理过程断断续续,经历了两周多的时间,中间各种尝试,总结如下.这篇文章分三部分: 1.问题的场景和处理过程:2.GC的一些理论东西:3.看懂GC的日志 先说一下问题吧 ...

  6. c++11のunique_lock和once_flag

    一. Unique _lock和lockguard一样,到那时比lockguard更加灵活,可以随时按照需要加锁开锁 std::unique_lock<std::mutex> locker ...

  7. java中 & ^ ~ 的运算

    java运算符 与(&).非(~).或(|).异或(^)   最近看HashMap源码,遇到了这样一段代码: 1 static final int hash(Object key) { 2 i ...

  8. Putty中的pscp和psftp的简明用法

    用习惯了putty,那是真心的方便啊,putty文件夹下其他的小兄弟也不能忽略啊. 以前的时候,从远程服务器下载个文件用winscp,后来,发现在putty文件夹里好像有一个 pscp和psftp,今 ...

  9. Dockerfile 规范

    https://time-track.cn/compile-docker-from-source.html 参考 https://time-track.cn/install-docker-on-ubu ...

  10. day6-基础函数的学习(一)

    今日份目录 1.函数的定义 2.函数的返回值 3.函数的参数 4.函数的局部变量与全局变量 5.名称空间与作用域 6.global与nonlocal 7.高阶函数 继续今日份总结!这个总结也晚了,哎, ...