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类和继承用法实例
Jul 07 Python
在Linux命令行终端中使用python的简单方法(推荐)
Jan 23 Python
Python复制Word内容并使用格式设字体与大小实例代码
Jan 22 Python
python正则表达式爬取猫眼电影top100
Feb 24 Python
Python Dataframe 指定多列去重、求差集的方法
Jul 10 Python
详解Python解决抓取内容乱码问题(decode和encode解码)
Mar 29 Python
Python的垃圾回收机制详解
Aug 28 Python
关于tf.TFRecordReader()函数的用法解析
Feb 17 Python
使用OpenCV对车道进行实时检测的实现示例代码
Jun 19 Python
关于pycharm 切换 python3.9 报错 ‘HTMLParser‘ object has no attribute ‘unescape‘ 的问题
Nov 24 Python
在python中实现导入一个需要传参的模块
May 12 Python
Python中npy和mat文件的保存与读取
Apr 24 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 ci框架验证码实例分析
2013/06/26 PHP
yii操作cookie实例简介
2014/07/09 PHP
ThinkPHP内置jsonRPC的缺陷分析
2014/12/18 PHP
php导入大量数据到mysql性能优化技巧
2014/12/29 PHP
PHP扩展开发入门教程
2015/02/26 PHP
php调用KyotoTycoon简单实例
2015/04/02 PHP
PHP动态地创建属性和方法, 对象的复制, 对象的比较,加载指定的文件,自动加载类文件,命名空间
2016/05/06 PHP
thinkPHP实现多字段模糊匹配查询的方法
2016/12/01 PHP
PHP实现腾讯与百度坐标转换
2017/08/05 PHP
YII2框架中使用RBAC对模块,控制器,方法的权限控制及规则的使用示例
2020/03/18 PHP
一个html5播放视频的video控件只支持android的默认格式mp4和3gp
2014/05/08 Javascript
什么是cookie?js手动创建和存储cookie
2014/05/27 Javascript
提高NodeJS中SSL服务的性能
2014/07/15 NodeJs
IntersectionObserver实现图片懒加载的示例
2017/09/29 Javascript
在 React、Vue项目中使用SVG的方法
2018/02/09 Javascript
解决linux下node.js全局模块找不到的问题
2018/05/15 Javascript
如何用RxJS实现Redux Form
2018/12/29 Javascript
详解vue更改头像功能实现
2019/04/28 Javascript
微信小程序实现原生步骤条
2019/07/25 Javascript
vue实现element表格里表头信息提示功能(推荐)
2019/11/20 Javascript
[06:25]DOTA2英雄梦之声_第17期_大地之灵
2014/06/20 DOTA
[41:13]完美世界DOTA2联赛PWL S2 Forest vs Rebirth 第一场 11.20
2020/11/20 DOTA
Python实现的逻辑回归算法示例【附测试csv文件下载】
2018/12/28 Python
Django使用中间件解决前后端同源策略问题
2019/09/02 Python
python3实现单目标粒子群算法
2019/11/14 Python
浅谈Python3实现两个矩形的交并比(IoU)
2020/01/18 Python
html5基础标签(html5视频标签 html5新标签用法)
2013/12/30 HTML / CSS
Envie de Fraise意大利:法国网上推出的孕妇装品牌
2020/10/18 全球购物
安全大检查实施方案
2014/02/22 职场文书
陈欧广告词
2014/03/14 职场文书
六查六看心得体会
2014/10/14 职场文书
2015圣诞节贺卡寄语
2015/03/24 职场文书
写给汽车4S店的创业计划书,拿来即用!
2019/08/09 职场文书
python字典的元素访问实例详解
2021/07/21 Python
SQL Server中搜索特定的对象
2022/05/25 SQL Server