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

 更新时间:2015年05月20日 15:46:09   作者:Gavin_dinggengjia  
这篇文章主要介绍了Python使用shelve模块实现简单数据存储的方法,涉及shelve模块实现数据存储的技巧,需要的朋友可以参考下

本文实例讲述了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字符串相关str

    详解python字符串相关str

    这篇文章主要为大家介绍了python字符串相关str,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • python开发微信服务号消息推送示例

    python开发微信服务号消息推送示例

    这篇文章主要为大家介绍了python开发微信服务号消息推送示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • 如何用用Python将地址标记在地图上

    如何用用Python将地址标记在地图上

    这篇文章主要介绍了如何用用Python将地址标记在地图上,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2021-02-02
  • python中end=" "的含义及说明

    python中end=" "的含义及说明

    这篇文章主要介绍了python中end=" "的含义及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • Python3.7将普通图片(png)转换为SVG图片格式(网站logo图标)动起来

    Python3.7将普通图片(png)转换为SVG图片格式(网站logo图标)动起来

    这篇文章主要介绍了Python3.7将普通图片(png)转换为SVG图片格式并且让你的网站Logo(图标)从此”动”起来,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • 用表格帮你了解Python数据类型

    用表格帮你了解Python数据类型

    这篇文章主要为大家介绍了Python数据类型,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助<BR>
    2022-01-01
  • python 接口实现 供第三方调用的例子

    python 接口实现 供第三方调用的例子

    今天小编就为大家分享一篇python 接口实现 供第三方调用的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • python爬虫进阶之协程详解

    python爬虫进阶之协程详解

    这篇文章主要介绍了python爬虫进阶之协程详解,coroutine中文翻译叫协程,在 Python 中昌指代为协程对象类型,可以将协程对象注册到时间循环中被调用,需要的朋友可以参考下
    2023-08-08
  • python请求域名requests.(url = 地址)报错

    python请求域名requests.(url = 地址)报错

    本文主要介绍了python请求域名requests.(url = 地址)报错,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • 使用Python实现为PDF文件添加图章

    使用Python实现为PDF文件添加图章

    在日常工作中,我们经常需要给PDF文档添加一些标识,比如公司的图章或水印图章,所以本文就来为大家详细介绍一下如何使用Python实现为PDF文件添加图章,需要的可以参考下
    2023-11-11

最新评论