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模块理解的更多相关文章

  1. os常用模块,json,pickle,shelve模块,正则表达式(实现运算符分离),logging模块,配置模块,路径叠加,哈希算法

    一.os常用模块 显示当前工作目录 print(os.getcwd()) 返回上一层目录 os.chdir("..") 创建文件包 os.makedirs('python2/bin ...

  2. Python学习 :json、pickle&shelve 模块

    数据交换格式 json 模块 json (JavaScript Object Notation)是一种轻量级的数据交换语言,以文字为基础,且易于让人阅读.尽管 json 是JavaScript的一个子 ...

  3. json & pickle & shelve 模块

    JSON表示的对象就是标准的JavaScript语言的对象,JSON和Python内置的数据类型对应如下: # json序列化 import json,time user={'name':'egon' ...

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

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

  5. python pickle 和 shelve模块

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

  6. shelve模块

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

  7. 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时间 ...

  8. 小白的Python之路 day5 shelve模块讲解

    shelve模块讲解 一.概述 之前我们说不管是json也好,还是pickle也好,在python3中只能dump一次和load一次,有什么方法可以向dump多少次就dump多少次,并且load不会出 ...

  9. python之shelve模块详解

    一.定义 Shelve是对象持久化保存方法,将对象保存到文件里面,缺省(即默认)的数据存储文件是二进制的. 二.用途 可以作为一个简单的数据存储方案. 三.用法 使用时,只需要使用open函数获取一个 ...

随机推荐

  1. 从 MySQL+MMM 到 MariaDB+Galera Cluster : 一个高可用性系统改造

    很少有事情比推出高可用性(HA)系统之后便经常看到的系统崩溃更糟糕.对于我们这个Rails运行机的团队来说,这个失效的HA系统是MySQL多主复制管理器(MMM). 我们已经找寻MMM的替代品有一段时 ...

  2. [转]Hibernate update和saveOrUpdate详解

    原文地址:http://www.iteye.com/topic/2712 先来点概念: 在Hibernate中,最核心的概念就是对PO的状态管理.一个PO有三种状态: 1.未被持久化的VO 此时就是一 ...

  3. 【HDU 2089】不要62

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 数位dp,参照了打野的博客 预处理出f数组,f[i][j]表示第i位为数字j时的可行的数字总数. 对于区间 ...

  4. C#-面向对象的三大特性——继承

    继承 注意事项: 继承语法:   类名:父类名 父类也称之为 基类 ,子类也可以成为 xxx的派生类 或 超类. 父类可以有无限个子类,子类只能有一个父类(亲爹),可以有无限个接口(干爹) 子类并不是 ...

  5. 让Chrome支持Ajax/$http方式读取本地文件

    在开发中经常写些小demo调试一下插件什么的 数据源又经常手动构造分离为一个单独的文件.用ajax或$http去访问时总是拒绝访问.这个时候可以给Chrome的快捷图标加启动参数,让浏览器允许js访问 ...

  6. 【POJ 3177】Redundant Paths(边双连通分量)

    求出每个边双连通分量缩点后的度,度为1的点即叶子节点.原图加上(leaf+1)/2条边即可变成双连通图. #include <cstdio> #include <cstring> ...

  7. 生活就像测试, BUG会越来越少,生活会越来越好!

    生活就像测试, BUG会越来越少,生活会越来越好!

  8. GraphX的三大图算法

    1. PageRank http://blog.csdn.net/hguisu/article/details/7996185 2. Connected Components 3. Triangle ...

  9. [日常训练]mod

    Description 给定$p_1,p_2,-,p_n,b_1,b_2,...,b_m$, 求满足$x\;mod\;p_1\;\equiv\;a_1,x\;mod\;p_2\;\equiv\;a_2 ...

  10. 订阅Jenkins的邮件列表,获取最新的信息

    进入https://jenkins.io/content/mailing-lists/ 点击感兴趣的话题 选择[archive]跳转到谷歌讨论组 最后,点击左上角的[Subscribe]即可加入Goo ...