Python_shelve模块
shelve:对象持久化的保存的模块,将对象保存到文件里 (默认的数据存储文件为二进制),可持久化任何pickle可支持的Python数据格式
shelve 中唯一的方法:
shelve.open(filename,flag = 'c', protocol = None , writebake = False)
| filename | 关联的文件路径 |
| flag | 'r' :以只读模式打开一个已经存在的数据存储文件 |
| 'w' :以读写模式打开一个已经存在的数据存储文件 | |
| 'c' :(默认)以读写模式打开一个数据存储文件,如果不存在则创建 | |
| 'n' :总是以读写模式打开并且创建一个新的空数据存储文件 | |
| protocol | 表示序列化数据所使用的协议,默认为 None(pickle v3) |
| writebake | 表示是否开启回写功能 |
1. 文件可以像字典一样存储key - value (注:key 必须为字符串,value 可以是任何数据类型)
import shelve
date = shelve.open('family.txt') # Python的自处理系统会自动生成三个文件
date['father'] = 'Presly' # 默认为创建并且写入“c”
date['mather'] = 'Vera'
date['baby'] = [123, ]
date.close()
m = shelve.open('family.txt', falg= 'r', writebake=True) # 当writebake设置为True时,文件里才能直接添加
print(m['baby']) m['baby'].append(345)
print(m['father'])
print('-------------')
for key, value in m.items(): # 以字典的格式
print(key, ':', value)
m.close()
[123]
Presly
-------------
father : Presly
mather : Vera
baby : [123,345]
2. shelve 的序列化
- 可以把类的数据序列化,然后再 反序列化出元素
- 与pickle不同的是,pickle只能按照dump顺序,load出元素,而shelve可以直接重复拿出不同或者相同存进文件的key值,
3. shelve 可以进行类似于库,增,删,改,查
import shelve def store_information(database):
info = {}
ID = input('Enter the ID number:')
info['name'] = input('Enter the name:') # 将name ,age , phone 存入字典info里
info['age'] = input('Enter the age:')
info['phone'] = input('Enter the phone:')
database[ID] = info # 用ID : info 存入 database文件 def lookup_information(database):
ID = input('Enter the ID:')
field = input('What would you like to know?(name,age,phone)')
field = field.strip().lower()
print(database[ID][field]) # 通过输入的ID与 field 直接打印结果 def print_help():
print('Please enter the help command:')
print('store :store informatinon to database')
print('lookup :look up information by numID')
print('quit :save information and quit')
print('? :print help command') def enter_command():
cmd = input('Enter command (? for help)')
cmd = cmd.strip().lower()
return cmd def main():
database = shelve.open('db.dat')
try:
while True:
cmd = enter_command()
if cmd == 'store':
store_information(database) # 当if函数结束,自动跳转到cmd = enter_command()行
elif cmd == 'lookup':
lookup_information(database)
elif cmd == '?':
print_help()
elif cmd == 'quit':
return # 跳出while循环
finally:
database.close() if __name__ == '__main__': main()
一个例子
例子来源于:https://blog.csdn.net/u010480899/article/details/52864518
Python_shelve模块的更多相关文章
- Python_shelve模块操作二进制文件
import shelve #导入shelve模块 fp=shelve.open('shelve_test.dat') #创建或打开二进制文件 zhangsan={'age':38,'sex':'Ma ...
- 铁乐学python_shelve模块详解
python序列化模块-shelve模块详解 shelve:vt. 将(书等)放置在架子上:搁置,将某事放到一旁不予考虑:将-搁在一边:装搁架于: 个人感觉有点像字典缓存?暂时搁置到一旁的意思? 研究 ...
- npm 私有模块的管理使用
你可以使用 NPM 命令行工具来管理你在 NPM 仓库的私有模块代码,这使得在项目中使用公共模块变的更加方便. 开始前的工作 你需要一个 2.7.0 以上版本的 npm ,并且需要有一个可以登陆 np ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- ES6模块import细节
写在前面,目前浏览器对ES6的import支持还不是很好,需要用bable转译. ES6引入外部模块分两种情况: 1.导入外部的变量或函数等: import {firstName, lastName, ...
- Python标准模块--ContextManager
1 模块简介 在数年前,Python 2.5 加入了一个非常特殊的关键字,就是with.with语句允许开发者创建上下文管理器.什么是上下文管理器?上下文管理器就是允许你可以自动地开始和结束一些事情. ...
- Python标准模块--Unicode
1 模块简介 Python 3中最大的变化之一就是删除了Unicode类型.在Python 2中,有str类型和unicode类型,例如, Python 2.7.6 (default, Oct 26 ...
- Python标准模块--Iterators和Generators
1 模块简介 当你开始使用Python编程时,你或许已经使用了iterators(迭代器)和generators(生成器),你当时可能并没有意识到.在本篇博文中,我们将会学习迭代器和生成器是什么.当然 ...
- 自己实现一个javascript事件模块
nodejs中的事件模块 nodejs中有一个events模块,用来给别的函数对象提供绑定事件.触发事件的能力.这个别的函数的对象,我把它叫做事件宿主对象(非权威叫法),其原理是把宿主函数的原型链指向 ...
随机推荐
- HDU - 1160 FatMouse's Speed 动态规划LIS,路径还原与nlogn优化
HDU - 1160 给一些老鼠的体重和速度 要求对老鼠进行重排列,并找出一个最长的子序列,体重严格递增,速度严格递减 并输出一种方案 原题等于定义一个偏序关系 $(a,b)<(c.d)$ 当且 ...
- $Django orm增删改字段、建表 ,单表增删改查,Django请求生命周期
1 orm介绍 ORM是什么 ORM 是 python编程语言后端web框架 Django的核心思想,“Object Relational Mapping”,即对象-关系映射,简称ORM. 一 ...
- SSL For Free 申请免费https SSL 凭证
打开 SSL For Free网站(https://www.sslforfree.com) ,在输入框中填入你要申请 Let’s Encrypt 凭证的网域名称,可以用空白来分隔不同的网址,例如[su ...
- Scientific Toolworks Understand
Scientific Toolworks Understand是一款定位于代码阅读的软件.界面用Qt开发的. 软件特性: 1.支持多语言:Ada, C, C++, C#, Java, FORTRAN, ...
- 前端 ----jQuery的属性操作
04-jQuery的属性操作 jquery的属性操作模块分为四个部分:html属性操作,dom属性操作,类样式操作和值操作 html属性操作:是对html文档中的属性进行读取,设置和移除操作.比如 ...
- Python Redis pipeline操作
Redis是建立在TCP协议基础上的CS架构,客户端client对redis server采取请求响应的方式交互. 一般来说客户端从提交请求到得到服务器相应,需要传送两个tcp报文. 设想这样的一个场 ...
- Golang 类型转换,断言和显式强制转换
1 前言 类型转换,可以用断言(只能使用在interface{}类型转换成其它类型)和显式类型强制转换(常规是用于基本类型) 2 代码 //graphql-go func(params graphql ...
- Python中加入中文注释
最近开发学习Pyton,当加入中文注释时,运行程序报错: File SyntaxError: Non-ASCII character , but no encoding declared; see h ...
- 【进阶3-1期】JavaScript深入之史上最全--5种this绑定全面解析(转)
这是我在公众号(高级前端进阶)看到的文章,现在做笔记 https://github.com/yygmind/blog/issues/20 this的绑定规则总共有下面5种. 1.默认绑定(严格/非严 ...
- swift 实践- 10 -- UIProgressView
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...