• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python使用shelve模块实现简单数据存储的方法

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python shelve模块,python中的shelve模块,python shelve,shelve模块,shelve等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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()

</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • 举例简单讲解Python中的数据存储模块shelve的用法
  • Python中的anydbm模版和shelve模版使用指南
  • Python使用shelve模块实现简单数据存储的方法
  • python pickle 和 shelve模块的用法
  • python pickle 和 shelve模块的用法

相关文章

  • 利用Python中的pandas库对cdn日志进行分析详解
  • Python 登录网站详解及实例
  • Python中的闭包详细介绍和实例
  • Python脚本处理空格的方法
  • Windows中使用wxPython和py2exe开发Python的GUI程序的实例教程
  • Python 列表(List)操作方法详解
  • tensorflow ckpt模型和pb模型获取节点名称,及ckpt转pb模型实例
  • Python 使用os.remove删除文件夹时报错的解决方法
  • Python中的os.path路径模块中的操作方法总结
  • Python内置的字符串处理函数整理

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Windows系统配置python脚本开机启动的3种方法分享
    • python中OrderedDict的使用方法详解
    • 利用Python开发实现简单的记事本
    • Python中使用OpenCV库来进行简单的气象学遥感影像计算
    • python实现搜索指定目录下文件及文件内搜索指定关键词的方法
    • python比较两个列表是否相等的方法
    • 给Python的Django框架下搭建的BLOG添加RSS功能的教程
    • Python中利用sorted()函数排序的简单教程
    • Python中的True,False条件判断实例分析
    • Python编程之属性和方法实例详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有