本篇文章给大家带来的内容是关于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()

python shelve模块_Python中shelve模块的简单介绍(附示例)相关推荐

  1. python shelve模块_Python中shelve模块

    Python中Shelve模块是对象持久化保存方法,将对象保存到文件里面,缺省(即默认)的数据存储文件是二进制的,可以作为一个简单的数据存储方案.使用时,只需要使用open函数获取一个shelf对象, ...

  2. 简述python中怎样导入模块_Python中导入模块的两种模式,import

    import import pandas import pandas as pd 使用函数方式:.(),或者.() 比如 pandas.read_csv("data/stock.csv&qu ...

  3. python xlrd课程_python中xlrd模块的使用详解

    一.xlrd的安装 打开cmd输入pip install xlrd安装完成即可 二.xlrd模块的使用 下面以这个工作簿为例 1.导入模块 import xlrd 2.打开工作薄 # filename ...

  4. python中自带的模块_python中的模块详解

    概念 python中的模块是什么?简而言之,在python中,一个文件(以".py"为后缀名的文件)就叫做一个模块,每一个模块在python里都被看做是一个独立的文件.模块可以被项 ...

  5. python中mysqldb模块_python中MySQLdb模块用法实例

    本文实例讲述了python中MySQLdb模块用法.分享给大家供大家参考.具体用法分析如下: MySQLdb其实有点像php或asp中连接数据库的一个模式了,只是MySQLdb是针对mysql连接了接 ...

  6. python pygame模块_python中pygame模块用法实例

    本文实例讲述了python中pygame模块用法,分享给大家供大家参考.具体方法如下: import pygame, sys from pygame.locals import * #set up p ...

  7. python从包中导入模块_Python中包,模块导入的方法

    Python中包,模块导入的方法 http://www.cnblogs.com/allenblogs/archive/2011/05/24/2055149.html 1. import modname ...

  8. python添加自定义模块_Python中添加自定义模块的方法

    Python中添加自定义模块的方法 发布时间:2020-07-17 14:01:03 来源:亿速云 阅读:95 作者:小猪 这篇文章主要讲解了Python中添加自定义模块的方法,内容清晰明了,对此有兴 ...

  9. python的config模块_python中ConfigParse模块的用法

    本文实例讲述了python中configparse模块的用法,分享给大家供大家参考.具体方法如下: 写配置一般用configparse.rawconfigparse类 读配置用configparse. ...

最新文章

  1. Tensorflow— word2vec
  2. 勘误表《网络规划设计师考试考点分析与真题详解》
  3. centos7 php 404,CentOS7 Apache环境配置自定义404错误页面怎么解决
  4. 总线接口与计算机通信(五)CAN总线
  5. java异常体系_JAVA异常体系结构详解
  6. 可观测性PHP秩判据,线性系统的可控性与可观测性
  7. tablednd保存 php,TableDnD-JavaScript中文网-JavaScript教程资源分享门户
  8. uva 11997(优先队列)
  9. 装饰模式(Decorate Pattern)
  10. 《深入浅出数据分析》读书心得与笔记
  11. python图灵机器人教程_Python-微信图灵机器人
  12. 保险场景化与场景即保险——新保险
  13. FPGA抗辐射加固方法
  14. 深度学习-8.实践方法论
  15. 考研二战日记-第16天小结
  16. 2022.12.23-Python100day-day05-面向对象编程
  17. java并发编程中常用的工具类 Executor
  18. java开发一个购物车实验,JAVAWEB购物车实验报告.doc
  19. [哈希]PAT1039 Course List for Student
  20. Vista 虚拟光驱解决方案之 Alcohol

热门文章

  1. OSChina 周三乱弹 —— 要不当程序员,准能拿小金人
  2. 自旋锁与互斥锁的区别
  3. 现代汽车减持格灵深瞳:至少套现3402万 仍持股近5%
  4. xrdp linux 3389 端口,在 Linux 中使用 xrdp - Azure Virtual Machines | Microsoft Docs
  5. mavonEditor代码块复制功能
  6. Microphone回音问题分析
  7. 干货 | 一文详解隐含狄利克雷分布(LDA)
  8. Dirichlet分布的推导与理解
  9. ameya360资讯:罗姆将参加2022年慕尼黑电子展
  10. daemonize(daemonized)