shelve是对pickle的封装

json & pickle是把所有的数据全部封装,一次性写入文件,而shelve可以把数据分类,以键值对的形式分别写入文件

shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式

序列化:

import shelve

f = shelve.open('shelve_test')  # 打开一个文件

names = ["alex", "rain", "test"]
info = {'name':'alex','age':22} f["names"] = names # 持久化列表
f['info_dic'] = info f.close()

反序列化

import shelve

d = shelve.open('shelve_test')  # 打开一个文件

print(d['names'])
print(d['info_dic']) #del d['test'] #还可以删除

shelve的更多相关文章

  1. python模块(shelve,xml,configparser,hashlib,logging)

    1.1shelve模块 shelve 模块比pickle模块简单,只有一个open函数,返回类似字典对象,可读可写:key必须为字符串, 而值可以是python所支持的数据类型. shelve模块主要 ...

  2. Shelve Instance 操作详解 - 每天5分钟玩转 OpenStack(38)

    Instance 被 Suspend 后虽然处于 Shut Down 状态,但 Hypervisor 依然在宿主机上为其预留了资源,以便在以后能够成功 Resume. 如果希望释放这些预留资源,可以使 ...

  3. python学习道路(day6note)(time &datetime,random,shutil,shelve,xml处理,configparser,hashlib,logging模块,re正则表达式)

    1.tiim模块,因为方法较多我就写在code里面了,后面有注释 #!/usr/bin/env python #_*_coding:utf-8_*_ print("time".ce ...

  4. shelve模块理解

    import shelve import sys def store_person(db): pid = input("Enter unique ID mnumber:") per ...

  5. pickle与shelve

    pickle Example 写入文件 import pickle integers = [1, 2, 3, 4, 5] with open('pickle-example.p', 'wb') as ...

  6. Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)

    OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname")  改变当前脚本工作目 ...

  7. python序列化: json & pickle & shelve 模块

    一.json & pickle & shelve 模块 json,用于字符串 和 python数据类型间进行转换pickle,用于python特有的类型 和 python的数据类型间进 ...

  8. python pickle 和 shelve模块

    pickle和shelve模块都可以把python对象存储到文件中,下面来看看它们的用法吧 1.pickle 写: 以写方式打开一个文件描述符,调用pickle.dump把对象写进去 dn = {'b ...

  9. shelve模块

    #coding:utf-8 __author__ = 'similarface' #email:similarface@outlook.com ''' shelve模块: 映射容器 存储对象,被存储的 ...

  10. python(6)- shelve模块

    前面学习了pickle,这两个可以将数据持久化存储到硬盘上,在实际应用中,我们可能会多次将数据dump到同一文件里,试一下: import pickle data = {'k1':123, 'k2': ...

随机推荐

  1. 自己写一个spring boot starter

    https://blog.csdn.net/liuchuanhong1/article/details/55057135

  2. 剑指offer例题——旋转数组的最小数字

    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转, ...

  3. How to Pronounce TH after N or Z

    How to Pronounce TH after N or Z Share Tweet Share Tagged With: Linking Consonant to Consonant The T ...

  4. springboot 项目 注意事项

    SpringBoot出现下列错误. Your ApplicationContext is unlikely to start due to a @ComponentScan of the defaul ...

  5. Linux Install redis

    1.将下载好的压缩包放到/usr/local目录下# tar xzf redis-3.0.2.tar.gz # cd redis-3.0.2 # make//--------------------- ...

  6. windows下如何查看端口,关闭端口,开启端口

    如何查看端口 在Windows 2000/XP/Server 2003中要查看端口,可以使用NETSTAT命令: “开始">"运行”>“cmd”,打开命令提示符窗口.在 ...

  7. [PHP]PHP缓存机制之Output Control

    ---------------------------------------------------------------------------------------------------- ...

  8. SQL Server--存在则更新问题

    在博客园看到一篇讨论特别多的文章“探讨SQL Server并发处理存在就更新七种解决方案”,这种业务需求很常见:如果记录存在就更新,不存在就插入. 最常见的做法: BEGIN TRANSACTION ...

  9. 1.Tomcat配置.md

    1.启动 解压缩安装包后,点击startup.bat,保持控制台窗口开启 浏览器中输入http://localhost:8080 后看到启动界面则表示启动成功 点击shutdown.bat则关闭Tom ...

  10. git初使用总结感悟

    ####首先说说git在工作中的一般流程1.首先就是进入新公司之后,要了解公司用的是什么代码托管,比如gitlab或者github(私有库花钱) 2.找技术同时把你加入到工作项目组并给你权限(一般都是 ...