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 相关文章推荐
通过C++学习Python
Jan 20 Python
Python os模块中的isfile()和isdir()函数均返回false问题解决方法
Feb 04 Python
Python中zfill()方法的使用教程
May 20 Python
python 换位密码算法的实例详解
Jul 19 Python
Python 获得13位unix时间戳的方法
Oct 20 Python
python微信公众号开发简单流程
Mar 23 Python
python爬虫获取小区经纬度以及结构化地址
Dec 30 Python
Python 串口读写的实现方法
Jun 12 Python
pyqt 实现在Widgets中显示图片和文字的方法
Jun 13 Python
python简单实现矩阵的乘,加,转置和逆运算示例
Jul 10 Python
使用Python的turtle模块画国旗
Sep 24 Python
python+selenium 简易地疫情信息自动打卡签到功能的实现代码
Aug 22 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中用foreach来操作数组的代码
2011/07/17 PHP
Zend Framework框架路由机制代码分析
2016/03/22 PHP
PHP单链表的实现代码
2016/07/05 PHP
php实现基于openssl的加密解密方法
2016/09/30 PHP
thinkPHP框架中执行原生SQL语句的方法
2017/10/25 PHP
JS实现浏览器菜单命令
2006/09/05 Javascript
js jquery数组介绍
2012/07/15 Javascript
jquery cookie实现的简单换肤功能适合小网站
2013/08/25 Javascript
javaScript中两个等于号和三个等于号之间的区别介绍
2014/06/27 Javascript
js弹出对话框方式小结
2015/11/17 Javascript
基于JavaScript实现div层跟随滚动条滑动
2016/01/12 Javascript
js实现做通讯录的索引滑动显示效果和滑动显示锚点效果
2017/02/18 Javascript
JavaScript之underscore_动力节点Java学院整理
2017/07/03 Javascript
angular4中关于表单的校验示例
2017/10/16 Javascript
Angular中点击li标签实现更改颜色的核心代码
2017/12/08 Javascript
基于模板引擎Jade的应用(详解)
2017/12/12 Javascript
通过vue-router懒加载解决首次加载时资源过多导致的速度缓慢问题
2018/04/08 Javascript
Angular使用Restful的增删改
2018/12/28 Javascript
JS中如何轻松遍历对象属性的方式总结
2019/08/06 Javascript
vue微信分享插件使用方法详解
2020/02/18 Javascript
js 闭包深入理解与实例分析
2020/03/19 Javascript
[44:41]Fnatic vs Liquid 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
python使用webbrowser浏览指定url的方法
2015/04/04 Python
python实现证件照换底功能
2019/08/20 Python
redis数据库及与python交互用法简单示例
2019/11/01 Python
Django中使用MySQL5.5的教程
2019/12/18 Python
修复iPhone的safari浏览器上submit按钮圆角bug
2012/12/24 HTML / CSS
英国最大的在线床超市:Bed Star
2019/01/24 全球购物
俄罗斯金苹果网上化妆品和香水商店:Goldapple
2019/12/01 全球购物
linux面试相关问题
2012/08/11 面试题
银行门卫岗位职责
2013/12/29 职场文书
污水厂厂长岗位职责
2014/01/04 职场文书
初中地理教学反思
2014/01/11 职场文书
给领导的致歉信范文
2014/01/13 职场文书
关于青春的演讲稿500字
2014/08/22 职场文书
依法行政工作汇报材料
2014/10/28 职场文书