python shelve模块
#coding=utf-
import shelve f = shelve.open("shelve_test")
f['info'] = "alex"
f["age"] = [,,,,,]
f["name"] = {"name":"alex","add":"sz"}
f.close() f = shelve.open("shelve_test")
# 这里这个f我们就可以理解为是一个字典对象,所以shelve存储数据就按照字典的方式存储数据 print(f.get("info"))
print(f.get("age"))
print(f.get("name"))
print(f['info'])
print(f.values())
print(f.items())
print(f.keys())
输出
alex
[, , , , , ]
{'name': 'alex', 'add': 'sz'}
alex
ValuesView(<shelve.DbfilenameShelf object at 0x000001A88D374208>)
ItemsView(<shelve.DbfilenameShelf object at 0x000001A88D374208>)
KeysView(<shelve.DbfilenameShelf object at 0x000001A88D374208>)
#coding=utf-
import shelve
with shelve.open("test_shelve.db") as f:
f["k1"] = {
"name":"xiaoming1",
"age":,
"address":"beijing1"
}
f["k2"] = {
"name":"xiaoming2",
"age":,
"address":"beijing2"
}
f["k3"] = {
"name":"xiaoming3",
"age":,
"address":"beijing3"
}
f["k4"] = ["小明1","小明2","小明3"]
with shelve.open("test_shelve.db") as f:
print(f["k1"]["name"])
print(f["k4"][])
输出
xiaoming1
小明1
#coding=utf-
import shelve
test_list=[,,,,]
test_dict={"aaa":,"bbb":}
s="xiaoming"
with shelve.open("shelve.ini","wc") as f:
f["k1"] = test_list
f["k2"] = test_dict
f["k3"] = s with shelve.open("shelve.ini","rc") as k:
print(k["k3"])
print(k["k2"])
print(k["k1"])
输出
xiaoming
{'aaa': , 'bbb': }
[, , , , ]
#coding=utf-
import shelve list = [, , ]
she = shelve.open('test.dat')
she['d'] = list
she['d'].append('f')
print(she['d']) list = [, , ]
she = shelve.open('test.dat')
she['d'] = list
temp = she['d']
temp.append('f')
she['d'] = temp
print(she['d'])
输出
[, , ]
[, , , 'f']
python shelve模块的更多相关文章
- Python shelve 模块
使用json或者pickle持久化数据,能dump多次,但load的话只能取到最新的dump, 因为先前的数据已经被后面dump的数据覆盖掉了. 如果想要实现dump多次不被覆盖,就可以想到使用she ...
- [Python shelve模块Error]bsddb.db.DBPageNotFoundError: (-30986, 'DB_PAGE_NOTFOUND: Requested page not found')
在用scrapy抓数据,用shelve保存时出现了这个Error,目标是储存一串unicode字符串组成的列表,exception代码是tempbase['joke']=joke_list,测试只要j ...
- Python shelve模块的使用方法
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = '人生入戏' import shelve,time #写 x = shelve. ...
- python序列化: json & pickle & shelve 模块
一.json & pickle & shelve 模块 json,用于字符串 和 python数据类型间进行转换pickle,用于python特有的类型 和 python的数据类型间进 ...
- python pickle 和 shelve模块
pickle和shelve模块都可以把python对象存储到文件中,下面来看看它们的用法吧 1.pickle 写: 以写方式打开一个文件描述符,调用pickle.dump把对象写进去 dn = {'b ...
- 小白的Python之路 day5 shelve模块讲解
shelve模块讲解 一.概述 之前我们说不管是json也好,还是pickle也好,在python3中只能dump一次和load一次,有什么方法可以向dump多少次就dump多少次,并且load不会出 ...
- Python 序列化模块(json,pickle,shelve)
json模块 JSON (JavaScript Object Notation):是一个轻量级的数据交换格式模块,受javascript对象文本语法启发,但不属于JavaScript的子集. 常用方法 ...
- python之shelve模块详解
一.定义 Shelve是对象持久化保存方法,将对象保存到文件里面,缺省(即默认)的数据存储文件是二进制的. 二.用途 可以作为一个简单的数据存储方案. 三.用法 使用时,只需要使用open函数获取一个 ...
- Python全栈之路----常用模块----序列化(json&pickle&shelve)模块详解
把内存数据转成字符,叫序列化:把字符转成内存数据类型,叫反序列化. Json模块 Json模块提供了四个功能:序列化:dumps.dump:反序列化:loads.load. import json d ...
随机推荐
- 清明 DAY 1
数学基础 Part 1. 高精度计算 Part 2. 模意义下的运算 mod 对一个数取模,其实就是取余数 注意: • 无除法运算 • ...
- webstorm实用快捷键
webstorm实用快捷键 Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*…*/ ) Shift+F6 重构-重命名 Ctrl+X 删除行 Ctrl+D 复制行 Ctrl+G 查找行 ...
- select2 AJAX获取数据
页面效果: index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"& ...
- (5keras自带的模型之间的关系)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署
其中: 1.VGG 网络以及从 2012 年以来的 AlexNet 都遵循现在的基本卷积网络的原型布局:一系列卷积层.最大池化层和激活层,最后还有一些全连接的分类层. 2.ResNet 的作者将 ...
- Codeforces 789D Weird journey - 欧拉路 - 图论
Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his mot ...
- CIL锁,GIL与线程池的区别,进程池和线程池,同步与异步
一.GIL锁 什么是GIL? 全局解释器锁,是加在解释器上的互斥锁 GC是python自带的内存管理机制,GC的工作原理:python中的内存管理使用的是应用计数,每个数会被加上一个整型的计数器,表示 ...
- Overture 5入门之如何设置延音线
延音线作为五线谱标记符合之一,是大家使用Overture时需要常用的一个基本标记符号.延音线是一条向上或向下弯曲的弧线,它的作用是将两个或两个以上的具有相同高音的音符来进行相连. 延音线作为再编曲演唱 ...
- C++max的使用方法
#include <iostream> //#include <algorithm>//std::min std::max #include <stdint.h> ...
- 【Python046--魔法方法:描述符】
一.描述符的定义: 描述符就是将特殊类型的类的实例指派给另外一个类的属性 1.举例: 特殊类型的类要实现以下三个方法中的其中一个或者全部实现 * __get__(self,instance,owner ...
- 【python002-设计小游戏】
用python设计第一个游戏 一.运行idle的快捷方式:F5 二.首先设计一款小游戏: print('-------开启python学习之旅--------')temp = input(" ...