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模块,用来给别的函数对象提供绑定事件.触发事件的能力.这个别的函数的对象,我把它叫做事件宿主对象(非权威叫法),其原理是把宿主函数的原型链指向 ...
随机推荐
- 18春季训练01-3/11 2015 ACM Amman Collegiate Programming Contest
Solved A Gym 100712A Who Is The Winner Solved B Gym 100712B Rock-Paper-Scissors Solved C Gym 100712C ...
- 【原创】大叔经验分享(37)CM清理磁盘空间
定期清理cloudera manager server的磁盘空间 1 停止Service Monitor和Host Monitor 2 删除日志 # /bin/rm /var/lib/cloudera ...
- Redis重大版本
Redis借鉴了Linux操作系统对于版本号的命名规则: 版本号第二位如果是奇数,则为非稳定版本(例如2.7.2.9.3.1),如果是偶数,则为稳定版本(例如2.6.2.8.3.0.3.2), 当前奇 ...
- Codeforces 1132G Greedy Subsequences [线段树]
洛谷 Codeforces 看到题解那么少就来发一篇吧-- 思路 看完题目一脸懵逼,感觉无从下手. 莫名其妙地想到笛卡尔树,但笛卡尔树好像并没有太大作用. 考虑把笛卡尔树改一下:每个点的父亲设为它的右 ...
- bzoj3730 震波 [动态点分治,树状数组]
传送门 思路 如果没有强制在线的话可以离线之后CDQ分治随便搞. 有了强制在线之后--可能可以二维线段树?然而我不会算空间. 然后我们莫名其妙地想到了动态点分治,然后这题就差不多做完了. 点分树有一个 ...
- vue打包项目后使用-webkit-line-clamp: 2;这个属性不生效?
在项目中要实现多行省略,-webkit-line-clamp: 2;打包后不生效,使用下面的方法: word-break: break-all; text-overflow: ellipsis; di ...
- numpy:dot与multiply
http://blog.csdn.net/iamzhangzhuping/article/details/52370241
- day 28 面向对象 三种特性之一 多态 鸭子类型 反射(反省)
多态是OOP的三大特征之一 字面意思:多种形态 多种状态 官方描述:不同的对象可以响应(调用)同一个方法 产生不同的结果(例如水的三相特征) 多态不是什么新技术 我们编写面向对象的程序时 其实就有多态 ...
- BeautifulSoup的基本用法
# -*- coding:UTF-8 -*- from urllib import request from bs4 import BeautifulSoup import re import sys ...
- 【sqli-labs】Less1~Less4
学习sql注入啦,一下都是我做sqli-labs时的笔记.可能有错误,如果有人发现了欢迎指正~~ 常用知识点: 1.mysql注释有三种:① #: 注释从#到行尾 ② --空格: 注释到行尾,注意-- ...