Python使用shelve模块实现简单数据存储的方法


Posted in Python onMay 20, 2015

本文实例讲述了Python使用shelve模块实现简单数据存储的方法。分享给大家供大家参考。具体分析如下:

Python的shelve模块提供了一种简单的数据存储方案,以dict(字典)的形式来操作数据。

#!/usr/bin/python
import sys, shelve
def store_person(db):
  """
  Query user for data and store it in the shelf object
  """
  pid = raw_input('Enter unique ID number:')
  person = {}
  person['name'] = raw_input('Enter name:')
  person['age'] = raw_input('Enter age:')
  person['phone'] = raw_input('Enter phone number:')
  db[pid] = person
def lookup_person(db):
  """
  Query user for ID and desired field, 
  and fetch the corresponding data 
  from the shelf object
  """
  pid = raw_input('Enter unique ID number:')
  temp = db[pid]
  field = raw_input('Please enter name, age or phone:')
  field.strip().lower()
  print field.capitalize() + ': ', temp[field]
def print_help():
  print 'The avaliable commands are:'
  print 'store  :Stores infomation about a person'
  print 'lookup  :Looks up a person form ID number'
  print 'quit   :Save changes and exit'
  print '?    :Prints this message'
def enter_command():
  cmd = raw_input('Enter command(? for help):')
  cmd = cmd.strip().lower()
  return cmd
def main():
  database = shelve.open('database')
  # database stores in current directory
  try:
    while True:
      cmd = enter_command()
      if cmd == 'store':
        store_person(database)
      elif cmd == 'lookup':
        lookup_person(database)
      elif cmd == '?':
        print_help()
      elif cmd == 'quit':
        return
  finally:
    database.close()
    # Close database in any condition
if __name__ == '__main__':
  main()

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python 抓取动态网页内容方案详解
Dec 25 Python
Python实现生成简单的Makefile文件代码示例
Mar 10 Python
asyncio 的 coroutine对象 与 Future对象使用指南
Sep 11 Python
PyCharm代码提示忽略大小写设置方法
Oct 28 Python
[原创]Python入门教程1. 基本运算【四则运算、变量、math模块等】
Oct 28 Python
详解django自定义中间件处理
Nov 21 Python
Python操作redis实例小结【String、Hash、List、Set等】
May 16 Python
Django REST Framework序列化外键获取外键的值方法
Jul 26 Python
原生python实现knn分类算法
Oct 24 Python
Python-numpy实现灰度图像的分块和合并方式
Jan 09 Python
pycharm激活方法到2099年(激活流程)
Sep 22 Python
用Python实现一个打字速度测试工具来测试你的手速
May 28 Python
Python使用matplotlib实现在坐标系中画一个矩形的方法
May 20 #Python
python获取指定目录下所有文件名列表的方法
May 20 #Python
Python使用reportlab将目录下所有的文本文件打印成pdf的方法
May 20 #Python
Python使用matplotlib绘制动画的方法
May 20 #Python
Python中subprocess模块用法实例详解
May 20 #Python
python检测某个变量是否有定义的方法
May 20 #Python
Python实现在matplotlib中两个坐标轴之间画一条直线光标的方法
May 20 #Python
You might like
php中设置多级目录session的问题
2011/08/08 PHP
JSON 学习之JSON in JavaScript详细使用说明
2010/02/23 Javascript
3种不同方式的焦点图轮播特效分享
2013/10/30 Javascript
JS实现局部选择打印和局部不选择打印
2014/04/03 Javascript
js函数模拟显示桌面.scf程序示例
2014/04/20 Javascript
Jquery Post处理后不进入回调的原因及解决方法
2014/07/15 Javascript
浅谈AngularJs指令之scope属性详解
2016/10/24 Javascript
js 中文汉字转Unicode、Unicode转中文汉字、ASCII转换Unicode、Unicode转换ASCII、中文转换
2016/12/06 Javascript
canvas快速绘制圆形、三角形、矩形、多边形方法介绍
2016/12/29 Javascript
Node.js的Mongodb使用实例
2016/12/30 Javascript
NodeJS简单实现WebSocket功能示例
2018/02/10 NodeJs
详解js location.href和window.open的几种用法和区别
2019/12/02 Javascript
[03:24]CDEC.Y赛前采访 努力备战2016国际邀请赛中国区预选赛
2016/06/25 DOTA
Python中lambda的用法及其与def的区别解析
2014/07/28 Python
在Python中操作时间之mktime()方法的使用教程
2015/05/22 Python
Python实现控制台输入密码的方法
2015/05/29 Python
Python的Flask框架应用程序实现使用QQ账号登录的方法
2016/06/07 Python
python实现的正则表达式功能入门教程【经典】
2017/06/05 Python
python中Switch/Case实现的示例代码
2017/11/09 Python
Python 限定函数参数的类型及默认值方式
2019/12/24 Python
html5 web本地存储将取代我们的cookie
2012/12/26 HTML / CSS
HTML5页面嵌入小程序没有返回按钮及返回页面空白的问题
2020/05/28 HTML / CSS
美国宠物商店:Wag.com
2016/10/25 全球购物
美国婴儿和儿童家具网上商店:ABaby.com
2018/07/02 全球购物
澳大利亚礼品卡商店:Gift Card Store
2019/06/24 全球购物
TOWER London官网:鞋子、靴子、运动鞋等
2019/07/14 全球购物
程序运行正确, 但退出时却"core dump"了,怎么回事
2014/02/19 面试题
网络体系结构及协议的定义
2014/03/13 面试题
软件测试工程师结构化面试题库
2016/11/23 面试题
高分子材料与工程专业推荐信
2013/12/01 职场文书
小学生国旗下演讲稿
2014/04/25 职场文书
项目经理助理岗位职责
2015/04/13 职场文书
如何使用Python实现一个简易的ORM模型
2021/05/12 Python
pytorch损失反向传播后梯度为none的问题
2021/05/12 Python
使用这 6个Vue加载动画库来减少我们网站的跳出率
2021/05/18 Vue.js
Python Django项目和应用的创建详解
2021/11/27 Python