Python中shelve模块的简单介绍(附示例)
本篇文章给大家带来的内容是关于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模块的简单介绍(附示例)的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

在Python中,如何通过字符串动态创建对象并调用其方法?这是一个常见的编程需求,尤其在需要根据配置或运行...

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途
