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 ...
随机推荐
- Censor SCU - 4438
frog is now a editor to censor so-called sensitive words (敏感词). She has a long text (p). Her job is ...
- 【js】手机浏览器端唤起app,没有app就去下载app 的方法
这种功能的作用: 1.一般公司有自己的app,而app是需要不断有新用户涌入才能持续运营,达到不错的收入.就需要使用这种方式进行引入新的用户. 2.一些内容在网页端体验不好,或者一些功能需要app内才 ...
- Docker学习笔记之编写 Docker Compose 项目
0x00 概述 通过阅读之前的小节,相信大家对 Docker 在开发中的应用已经有了一定的了解.作为一款实用的软件,我们必须回归到实践中来,这样才能更好地理解 Docker 的实用逻辑和背后的原理.在 ...
- css盒子模型、边框border、外边距margin、填充padding、轮廓outline
盒子模型:盒子默认的宽度为容器的宽度,也可以自省设定宽度,高度根据内容适应,也可以自行设定高度.min-height设定最小高度 一个盒子包括外边距.边框.内边距和实际内容 Margin(外边距):清 ...
- 如何在Windows中通过Cygwin来使用Linux命令行
PowerShell的出现让Windows的命令行工具有了很大的改进.但是多年以来,Linux一直拥有很多有用的终端.在这里通过Cygwin你可以同时拥有上面两种命令行工具,Cygwin是一个可以在W ...
- bzoj 3237 连通图 - 并查集 - 线段树
Input Output Sample Input 4 5 1 2 2 3 3 4 4 1 2 4 3 1 5 2 2 3 2 1 2 Sample Output Connected Disconne ...
- Jbarcode 条形码生成工具
一.准备jar包 https://sourceforge.net/projects/jbcode/?source=typ_redirect 二.编写工具类 package com.example.de ...
- 启动maven的web项目
一.可以通过在pom中配置tomcat插件. 首先要确保你的仓库里有这个插件,然后按如下配置. <build> <plugins> <!-- 配置Tomcat插件 --& ...
- Python3 tkinter基础 Button bg 按钮的背景颜色
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Python 正则表达式学习
摘要 在正则表达式中,如果直接给出字符,就是精确匹配. {m,n}? 对于前一个字符重复 m到 n 次,并且取尽可能少的情况 在字符串'aaaaaa'中,a{2,4} 会匹配 4 个 a,但 a{2, ...