python入门系列15―——shelve
来源:互联网 发布:知乎手机如何发文章 编辑:程序博客网 时间:2024/05/20 10:14
欢迎前往我的个人博客
如果只需要一个简单的存储方案,我们可以使用shelve,高级的我们可以使用SQLite或者pySQLite。
shelve模块都是以键值对的形式保存数据,key必须为字符串,而值可以是python所支持的数据类型
获取一个shelve对象,没有会创建:
sh=shelve.open('Mydb.dat')插入数据:
import shelvesh=shelve.open('Mydb.dat')sh['a']=['a','b']print sh['a']接下来看这个例子:
import shelvesh=shelve.open('Mydb.dat')sh['a']=['a','b']sh['a'].append('c')print sh['a']存储的d到哪里去了呢?其实很简单,d没有写回,你把['a', 'b', 'c']存到了x,当你再次读取s['x']的时候,s['x']只是一个拷贝,而你没有将拷贝写回,所以当你再次读取s['x']的时候,它又从源中读取了一个拷贝,所以,你新修改的内容并不会出现在拷贝中,解决的办法就是,第一个是利用一个缓存的变量,如下所示
import shelvesh=shelve.open('Mydb.dat')sh['a']=['a','b']tmp=sh['a']tmp.append('c')sh['a']=tmpprint sh['a']
删除shelve对象中的某个键值对:
del sh['a']遍历所有数据:
for item in sh.items(): print('键[{}] = 值[{}]'.format(item[0], sh[item[0]]))关闭shelve对象:
sh.close()
阅读全文
0 0
- python入门系列15―——shelve
- Python——Shelve模块
- python shelve
- python入门系列19―——GUI wxpython入门
- python入门系列20―——GUI Tkinter入门
- python基础教程_学习笔记18:标准库:一些最爱——shelve
- python shelve编程
- Python: 初识shelve module
- python(8) shelve
- Python模块之shelve
- python模块:shelve
- Python-shelve模块
- python入门系列1―——helloworld
- python入门系列2―——运算符
- python入门系列3―——数据类型
- python入门系列4―——语句
- python入门系列5―——时间日期
- python入门系列6―——函数
- bootstrapvalidator remote ajax使用
- JavaSE中Swing类中的一些典型的组件
- Android智能下拉刷新框架-SmartRefreshLayout
- 常用函数一览
- HTTP简介
- python入门系列15―——shelve
- Oracle 角色及权限整理
- 矩阵旋转的问题
- XlistView--MylistViewDemo
- 贝塞尔曲线_简单理解入门_lemonen
- JZ2440 打开telnet服务
- scala外部排序算法
- 4556: [Tjoi2016&Heoi2016]字符串(后缀自动机做法)
- Spring注入值(Value注解)