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 16 Python
Python中的深拷贝和浅拷贝详解
Jun 03 Python
python生成excel的实例代码
Nov 08 Python
为什么入门大数据选择Python而不是Java?
Mar 07 Python
win8.1安装Python 2.7版环境图文详解
Jul 01 Python
解决Python3 控制台输出InsecureRequestWarning问题
Jul 15 Python
使用python实现数组、链表、队列、栈的方法
Dec 20 Python
Python新手学习函数默认参数设置
Jun 03 Python
python破解同事的压缩包密码
Oct 14 Python
python pymysql库的常用操作
Oct 16 Python
Python web框架(django,flask)实现mysql数据库读写分离的示例
Nov 18 Python
python-图片流传输的思路及示例(url转换二维码)
Dec 21 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
Body是什么,该怎么喝出咖啡里的口感
2021/03/03 咖啡文化
php使用sql数据库 获取字段问题介绍
2013/08/12 PHP
phpmailer在服务器上不能正常发送邮件的解决办法
2014/07/08 PHP
PHP语法自动检查的Vim插件
2014/08/11 PHP
详谈PHP文件目录基础操作
2014/11/11 PHP
100多行PHP代码实现socks5代理服务器[2]
2016/05/05 PHP
25个好玩的JavaScript小游戏分享
2011/04/22 Javascript
jQuery.query.js 取参数的两点问题分析
2012/08/06 Javascript
JQuery实现表格中相同单元格合并示例代码
2013/06/26 Javascript
jQuery无刷新切换主题皮肤实例讲解
2015/10/21 Javascript
基于JavaScript实现鼠标向下滑动加载div的代码
2016/08/31 Javascript
AngularJS 文件上传控件 ng-file-upload详解
2017/01/13 Javascript
教你用十行node.js代码读取docx的文本
2017/03/08 Javascript
ReactNative中使用Redux架构总结
2017/12/15 Javascript
select2 ajax 设置默认值,初始值的方法
2018/08/09 Javascript
angularjs1.5 组件内用函数向外传值的实例
2018/09/30 Javascript
vue单页面实现当前页面刷新或跳转时提示保存
2018/11/02 Javascript
JavaScript"模拟事件"的注意要点详解
2019/02/13 Javascript
[46:48]DOTA2上海特级锦标赛A组小组赛#2 Secret VS CDEC第三局
2016/02/25 DOTA
[11:42]2018DOTA2国际邀请赛寻真——OG卷土重来
2018/08/17 DOTA
[52:06]完美世界DOTA2联赛决赛日 Inki vs LBZS 第一场 11.08
2020/11/10 DOTA
python自动化工具日志查询分析脚本代码实现
2013/11/26 Python
python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法
2015/05/15 Python
TensorFlow实现MLP多层感知机模型
2018/03/09 Python
浅谈django orm 优化
2018/08/18 Python
python 对类的成员函数开启线程的方法
2019/01/22 Python
自学python的建议和周期预算
2019/01/30 Python
Python3.7基于hashlib和Crypto实现加签验签功能(实例代码)
2019/12/04 Python
python 两种方法删除空文件夹
2020/09/29 Python
canvas实现按住鼠标移动绘制出轨迹的示例代码
2018/02/05 HTML / CSS
日本最佳原创设计品牌:Felissimo(芬理希梦)
2019/03/19 全球购物
J2EE面试题
2016/03/14 面试题
便利店投资创业计划书
2014/02/08 职场文书
法人授权委托书范本
2014/09/17 职场文书
医院病假条怎么写
2015/08/17 职场文书
基于Golang 高并发问题的解决方案
2021/05/08 Golang