shelve模块理解
import shelve
import sys
def store_person(db):
pid = input("Enter unique ID mnumber:")
person = {}
person['name'] = input("Enter name:")
person['age'] = input("Enter age:")
person['phone'] = input("Enter phone number:")
db[pid] = person def lookup_perosn(db):
pid = input("Enter ID number:")
field = input("What would you like to know? (name,age,phone)")
filed = field.strip().lower()
print(field.capitalize() + ':',db[pid][field]) def print_help():
print("The available commons are:")
print("store:Looks up a person from ID number")
print("lookup:Looks up a person from ID number")
print("quit:save changes and exit")
print("?:Print this message") def enter_command():
cmd = input("Enter command(? for help):")
cmd = cmd.strip().lower()
return cmd def main():
database = shelve.open('testdata.dat')
try:
while True:
cmd = enter_command()
if cmd == 'store':
store_person(database)
elif cmd == 'lookup':
lookup_perosn(database)
elif cmd == '?':
print_help()
elif cmd == 'quit':
return
finally:
database.close()
if __name__ == '__main__':
main()
转载至:http://www.cnblogs.com/xiaoli2018/p/4423460.html
shelve模块理解的更多相关文章
- os常用模块,json,pickle,shelve模块,正则表达式(实现运算符分离),logging模块,配置模块,路径叠加,哈希算法
一.os常用模块 显示当前工作目录 print(os.getcwd()) 返回上一层目录 os.chdir("..") 创建文件包 os.makedirs('python2/bin ...
- Python学习 :json、pickle&shelve 模块
数据交换格式 json 模块 json (JavaScript Object Notation)是一种轻量级的数据交换语言,以文字为基础,且易于让人阅读.尽管 json 是JavaScript的一个子 ...
- json & pickle & shelve 模块
JSON表示的对象就是标准的JavaScript语言的对象,JSON和Python内置的数据类型对应如下: # json序列化 import json,time user={'name':'egon' ...
- python序列化: json & pickle & shelve 模块
一.json & pickle & shelve 模块 json,用于字符串 和 python数据类型间进行转换pickle,用于python特有的类型 和 python的数据类型间进 ...
- python pickle 和 shelve模块
pickle和shelve模块都可以把python对象存储到文件中,下面来看看它们的用法吧 1.pickle 写: 以写方式打开一个文件描述符,调用pickle.dump把对象写进去 dn = {'b ...
- shelve模块
#coding:utf-8 __author__ = 'similarface' #email:similarface@outlook.com ''' shelve模块: 映射容器 存储对象,被存储的 ...
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- 小白的Python之路 day5 shelve模块讲解
shelve模块讲解 一.概述 之前我们说不管是json也好,还是pickle也好,在python3中只能dump一次和load一次,有什么方法可以向dump多少次就dump多少次,并且load不会出 ...
- python之shelve模块详解
一.定义 Shelve是对象持久化保存方法,将对象保存到文件里面,缺省(即默认)的数据存储文件是二进制的. 二.用途 可以作为一个简单的数据存储方案. 三.用法 使用时,只需要使用open函数获取一个 ...
随机推荐
- jQuery 图片等比缩放
$(function(){ $('.img-box img').load(function(){ var w = $(this).width(); var h =$(this).height(); i ...
- html-div中内容自动换行
<div style='width: 100px;display:block;word-break: break-all;word-wrap: break-word;'> 内容超出div宽 ...
- 控件(文本类): AutoSuggestBox
Controls/TextControl/AutoSuggestBoxDemo.xaml <Page x:Class="Windows10.Controls.TextControl.A ...
- iOS推送通知
推送通知 此通知非彼通知. NSNotification是抽象的,看不见的,但是可以监听,属于观察者模式的一种设计模式. 推送通知是可见的,能用肉眼看见的,是真正的和用户打交道的通知. 推送通知分为两 ...
- Android中对内存和外存的读写
首先给大家介绍使用文件如何对数据进行存储,Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的实现过程与在J2SE环境中保存数据到文件中是一样的. public ...
- UDP编程中client和server中使用recvfrom和sendto的区别
client中: sendto(sfd,buf,strlen(buf),0,(struct sockaddr *)&saddr,len); recvfrom(sfd,buf ...
- springMVC数据验证出现404错误解决办法
今天使用springMVC的数据验证的时候,看似很简单的东西,却有一个很大的陷阱:提交空表单的时候总是出现404错误,但是后台却不给你报任何错.遇到这个错误这个很苦恼,搞了几小时,今天记录并分享一下解 ...
- hdu5047 找规律+欧拉公式
题意:在一个正方形内画n个M,求最多能分成多少个平面 sol:这种求划分成多少个平面的题第一反应肯定是欧拉公式: 二维平面上的欧拉公式:V+F-E=1 (V:Vertices,F:Faces,E:Ed ...
- 洛谷P1565 牛宫
题目描述 AP 神牛准备给自己盖一座很华丽的宫殿.于是,他看中了一块N*M 的矩形空地. 空地中每个格子都有自己的海拔高度.AP 想让他的宫殿的平均海拔在海平面之上(假设 海平面的高度是0,平均数都会 ...
- Unity连Photon服务器入门详解
Photon是目前比较好用的游戏服务器.目前网上对于Photon的服务器讲解比较少,最近也对Photon做了初步的了解,做一个极其详细的入门. 首先就是得下载Photon咯 https://www.p ...