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中的一些类型转换函数小结
Feb 10 Python
从局部变量和全局变量开始全面解析Python中变量的作用域
Jun 16 Python
解决python文件字符串转列表时遇到空行的问题
Jul 09 Python
python 遍历目录(包括子目录)下所有文件的实例
Jul 11 Python
Python基于Tkinter模块实现的弹球小游戏
Dec 27 Python
通过实例了解Python str()和repr()的区别
Jan 17 Python
在pycharm中实现删除bookmark
Feb 14 Python
python 读txt文件,按‘,’分割每行数据操作
Jul 05 Python
python 开心网和豆瓣日记爬取的小爬虫
May 29 Python
pytorch 两个GPU同时训练的解决方案
Jun 01 Python
Python图像处理库PIL详细使用说明
Apr 06 Python
基于PyQt5制作一个群发邮件工具
Apr 08 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中的超全局变量
2006/10/09 PHP
一个php作的文本留言本的例子(三)
2006/10/09 PHP
PHP中通过ADO调用Access数据库的方法测试不通过
2006/12/31 PHP
PHP运行环境配置与开发环境的配置(图文教程)
2013/06/04 PHP
编写php应用程序实现摘要式身份验证的方法详解
2013/06/08 PHP
PHP函数超时处理方法
2016/02/14 PHP
php实现的统计字数函数定义与使用示例
2017/07/26 PHP
javascript之对系统的toFixed()方法的修正
2007/05/08 Javascript
Enter回车切换输入焦点实现思路与代码兼容各大浏览器
2014/09/01 Javascript
JQuery表单验证插件EasyValidator用法分析
2014/11/15 Javascript
JavaScript判断是否是微信浏览器
2016/06/13 Javascript
bootstrap3 dialog 更强大、更灵活的模态框
2017/04/20 Javascript
HTML5实现微信拍摄上传照片功能
2017/04/21 Javascript
详解JS预解析原理
2020/06/16 Javascript
Javascript基于OOP实实现探测器功能代码实例
2020/08/26 Javascript
Python中函数的多种格式和使用实例及小技巧
2015/04/13 Python
剖析Python的Tornado框架中session支持的实现代码
2015/08/21 Python
python开发之文件操作用法实例
2015/11/13 Python
Python实现统计代码行的方法分析
2017/07/12 Python
基于python(urlparse)模板的使用方法总结
2017/10/13 Python
django实现用户登陆功能详解
2017/12/11 Python
解决pandas中读取中文名称的csv文件报错的问题
2018/07/04 Python
python 去除二维数组/二维列表中的重复行方法
2019/01/23 Python
Python 整行读取文本方法并去掉readlines换行\n操作
2020/09/03 Python
Python绘制数码晶体管日期
2021/02/19 Python
东南亚地区最大的购物网站Lazada新加坡站点:Lazada.sg
2016/07/17 全球购物
捷克浴室和厨房设备购物网站:SIKO
2018/08/11 全球购物
数据库面试要点基本概念
2013/10/31 面试题
法学专业应届生求职信
2013/10/16 职场文书
优秀经理事迹材料
2014/02/01 职场文书
敬老院标语
2014/06/27 职场文书
学生病假条怎么写
2015/08/17 职场文书
如何书写读后感?(附范文)
2019/07/26 职场文书
总结Python常用的魔法方法
2021/05/25 Python
Redis命令处理过程源码解析
2022/02/12 Redis
零基础学java之带参数以及返回值的方法
2022/04/10 Java/Android