函数和常用模块【day06】:shelve模块(五)
本节内容
1、简述
2、shelve概念
3、shelve模块使用
4、总结
一、简述
之前我们说不管是json也好,还是pickle也好,在python3中只能dump一次和load一次,不能dump多次,和load多次,但是我们真想要dump多次和load多次怎么办呢,并且能事项数据的持久化呐?好吧,今天我们就来说说这个shelve模块。
二、shelve概念
1、持久化
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import shelve #导入shelve模块def stu_data(name,age): #定义一个函数 print("register stu:",name,age)name = ["test","zhang","qi","gao"] #定义一个列表info = { "name":"zhangqigao","age":18} #定义一个字典with shelve.open("shelve_test") as d: d["test"] = name #持久化列表 d["info"] = info #持久化字典 d["func"] = stu_data #持久化函数 |
代码执行结果:
生成三个文件夹,分别是:shelve_test.dir、shelve_test.dat、shelve_test.bak
①shelve_test.dir内容
|
1
2
3
|
'test', (0, 50)'func', (1024, 24)'info', (512, 48) |
②shelve_test.dat内容
|
1
2
3
4
5
6
|
�]q (X testqX zhangqX qiqX gaoqe.�}q (X nameqX zhangqigaoqX ageqKu.�c__main__stu_dataq . |
③shelve_test.bak内容
|
1
2
3
|
'test', (0, 50)'func', (1024, 24)'info', (512, 48) |
2、解析文件内容
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import shelvedef stu_data(name,age): #这边一定要定义相同名字的函数,不然执行报错 print("stu:",name,age)with shelve.open("shelve_test") as f: print(f['test']) #解析列表 print(f['info']) #解析字典 print(f["func"]("zhangqsan",22)) #解析函数#输出['test', 'zhang', 'qi', 'gao']{'age': 18, 'name': 'zhangqigao'}stu: zhangqsan 22None |
三、shelve模块使用
3.1、常用方法
|
1
2
3
4
5
6
7
8
9
10
11
12
|
>>> import shelve>>> d = shelve.open("shelve_test")>>> dir(d)['_MutableMapping__marker', '__abstractmethods__', '__class__', '__contains__', '__del__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__','__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_abc_cache', '_abc_negative_cache','_abc_negative_cache_version', '_abc_registry', '_protocol', 'cache', 'clear', 'close', 'dict', 'get', 'items', 'keyencoding', 'keys', 'pop', 'popitem', 'setdefault', 'sync', 'update','values', 'writeback'] |
3.2、update
说明:update方法是如果序列化的值存在,则更新,如果不存在,则新增,用法:update({key:序列化对象})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#dumps到文件中import shelveinfo = { "name":"zhangqigao", "age":18}with shelve.open("shelve_test") as d: d['qigaotest'] = info #变量存在 d.update({'qigaotest':"shuaigaogao"}) #更新已经key为"qigaotest"的值#loads到内存中import shelvewith shelve.open("shelve_test") as f: print(f.get("qigaotest"))#输出shuaigaogao |
3.3、get
说明:把文件中的值load到内存中时,通过get它的key值获取
|
1
2
3
4
5
6
7
|
import shelvewith shelve.open("shelve_test") as f: print(f.get("qigaotest")) #或者是f["qigaotest"]#输出shuaigaogao |
注意:如果是通过f["qigaotest"]这种方法取,如果值不存在则会报错,通过get去取,不存在,则会返回none
四、总结
- shelve模块是一个简单的key,value将内存数据通过文件持久化的模块。
- shelve模块可以持久化任何pickle可支持的python数据格式。
- shelve就是pickle模块的一个封装。
- shelve模块是可以多次dump和load。
函数和常用模块【day06】:shelve模块(五)的更多相关文章
- python常用模块之shelve模块
python常用模块之shelve模块 shelve模块是一个简单的k,v将内存中的数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据类型 我们在上面讲json.pickle ...
- logging模块、sys模块、shelve模块
一.logging模块 1.logging模块就是用于记录日志的,日志就是记录某个时间点,发生的事情. 2.记录日志是为了日后来复查,提取有用的信息. 3.如何去记录日志:可以直接打开文件,记录信息, ...
- Python之路(第十五篇)sys模块、json模块、pickle模块、shelve模块
一.sys模块 1.sys.argv 命令行参数List,第一个元素是程序本身路径 2.sys.exit(n) 退出程序,正常退出时exit(0) 3.sys.version . sys.maxint ...
- python16_day06【类、RE模块、subprocess模块、xml模块、shelve模块】
一.shelve模块 import shelve # 基于pickle模块, d = shelve.open('shelve_test') class Test(object): def __init ...
- Python基础(12)_python模块之sys模块、logging模块、序列化json模块、pickle模块、shelve模块
5.sys模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 ...
- python 之 random 模块、 shutil 模块、shelve模块、 xml模块
6.12 random 模块 print(random.random()) (0,1)----float 大于0且小于1之间的小数 print(random.randint(1,3)) [1,3] 大 ...
- 序列化模块— json模块,pickle模块,shelve模块
json模块 pickle模块 shelve模块 序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. # 序列化模块 # 数据类型转化成字符串的过程就是序列化 # 为了方便存储和网 ...
- day5模块学习--shelve模块
shelve模块 shelve类似于一个key-value数据库,可以很方便的用来保存Python的内存对象,其内部使用pickle来序列化数据,简单来说,使用者可以将一个列表.字典.或者用户自定义的 ...
- shelve模块,sys模块,logging模块
1.shelve模块 用于序列化的模块,shelve模块比pickle模块简单,只有open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型. impor ...
随机推荐
- Redis的五种数据类型
官方的几篇很好的文章: https://redis.io/topics/data-types https://redis.io/topics/data-types-intro https://redi ...
- vCenter简单查看多少虚拟机在开机状态和一共多少虚拟机
vCenter 界面上面不好找 具体的开机 运行数目 但是数据库里面比较好差 登录vCenter的数据库. 查看表主要是 查看正在开机的虚拟机 select * from dbo.VPX_VM WHE ...
- FuelPHP 系列(六) ------ CURD 增删改查
一.create $article = new Model_Article(); // 或 $article = Model_Article::forge(); // 保存数据,返回新增数据 id $ ...
- 基于C#.NET的高端智能化网络爬虫(二)(攻破携程网)
本篇故事的起因是携程旅游网的一位技术经理,豪言壮举的扬言要通过他的超高智商,完美碾压爬虫开发人员,作为一个业余的爬虫开发爱好者,这样的言论我当然不能置之不理.因此就诞生了以及这一篇高级爬虫的开发教程. ...
- Fantacy团队周四站立会议
词频分析模型 1.会议时间:2016年3月31日12:07~12:30. 持续时长:23分钟 会议参加成员:组长:杨若鹏 http://www.cnblogs.com/robinYangRP/ 组员: ...
- pandas文件写入读取操作
#encoding:utf8 import pandas as pd import numpy as np from pylab import * df=pd.read_csv("./pat ...
- map内置函数分析所得到的思路
map:会根据提供的函数对指定序列做映射. map(func, *iterables) --> map object Make an iterator that computes the fun ...
- BZOJ3028 食物(生成函数)
显然构造出生成函数:则有f(x)=(1+x2+x4+……)·(1+x)·(1+x+x2)·(x+x3+x5+……)·(1+x4+x8+……)·(1+x+x2+x3)·(1+x)·(1+x3+x6+…… ...
- Intent 传递数据
使用INtent 在页面之间跳转,数据传递是必须的,我们可以直接在intent 对象上放置基本数据类型的数据,也可以放置字符串和其他数据类型数据.对于其他数据类型,实现了Parcelable 或Ser ...
- qrcode模块简单使用
函数式自动生成二维码 import qrcode img = qrcode.make("hello world!") img.get_image().show() img.save ...