shelve中有用的函数就是open(),但是下面编写的数据库函数中调用路径是经常出错,如果直接调用一个从来没有用过的文件却能正常运行,暂时没有找出原因。

调用shelve.open()会返回一个shelf对象用来存储内容,将它当做一个普通的字典来存储数据(字典的键一定要是字符串),在存储完毕之后要调用close()函数,关闭文件。

注意为了正确的使用shelve模块修改存储的对象,必须将临时变量绑定到获得的副本上,并且在修改后重新存储这个副本。或者直接将open()中writeback参数设为True。

下面是利用shelve模块建立的小型数据库

 #encoding=utf-8
__author__ = 'heng'
#简单的数据库应用程序 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 ID number: ')
field = raw_input('What would you like to know? (name, age, phone) ')
field = field.strip().lower()
print field.capitalize() + ':', \
db[pid][field] def print_help():
print 'The available commons are: '
print 'store :Stores information about a person'
print 'lookup :Looks up a person from ID number'
print 'quit :Save changes and exit'
print '? :Print this message' def enter_command():
cmd = raw_input('Enter command (? for help): ')
cmd = cmd.strip().lower()
return cmd def main():
database = shelve.open('testdata.dat')
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()
if __name__ == '__main__': main()

Python中的shelve模块的更多相关文章

  1. Python中的random模块,来自于Capricorn的实验室

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  2. Python中的logging模块

    http://python.jobbole.com/86887/ 最近修改了项目里的logging相关功能,用到了python标准库里的logging模块,在此做一些记录.主要是从官方文档和stack ...

  3. Python中的random模块

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  4. 浅析Python中的struct模块

    最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概了解了,在这里做一下简单的总结. 了解c语言 ...

  5. python中的StringIO模块

    python中的StringIO模块 标签:python StringIO 此模块主要用于在内存缓冲区中读写数据.模块是用类编写的,只有一个StringIO类,所以它的可用方法都在类中.此类中的大部分 ...

  6. python中的select模块

    介绍: Python中的select模块专注于I/O多路复用,提供了select  poll  epoll三个方法(其中后两个在Linux中可用,windows仅支持select),另外也提供了kqu ...

  7. Python中的re模块--正则表达式

    Python中的re模块--正则表达式 使用match从字符串开头匹配 以匹配国内手机号为例,通常手机号为11位,以1开头.大概是这样13509094747,(这个号码是我随便写的,请不要拨打),我们 ...

  8. python中的shutil模块

    目录 python中的shutil模块 目录和文件操作 归档操作 python中的shutil模块 shutil模块对文件和文件集合提供了许多高级操作,特别是提供了支持文件复制和删除的函数. 目录和文 ...

  9. Python中使用operator模块实现对象的多级排序

    Python中使用operator模块实现对象的多级排序 今天碰到一个小的排序问题,需要按嵌套对象的多个属性来排序,于是发现了Python里的operator模块和sorted函数组合可以实现这个功能 ...

随机推荐

  1. List去重复数据

    for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {  for ( int j = list.size() - 1 ; j > i; j -- ...

  2. ABAQUS学习记录1——用户子程序综述

    概述 ABAQUS提供了相当丰富的单元类型,材料属性等数据库可供用户选择,但是工程问题是千变万化的,为了满足用户的特殊工程要求,ABAQUS为用户提供了强大而又灵活的用户子程序接口(USER SUBR ...

  3. cmd启动MySQL服务器发生错误

    Mysql net start mysql启动,提示发生系统错误 5 拒绝访问  原文:https://blog.csdn.net/angel_guoo/article/details/7919037 ...

  4. dotTrace激活服务器

    http://active.09l.me IntelliJ IDEA 7.0 或 更高DataGrip 1.0或更高ReSharper 3.1 或更高ReSharper Cpp 1.0 或更高dotT ...

  5. docker-compose文件语法解析(v3.x)

    文件配置 compose文件是一个定义服务(service).网络(network)和卷(volume)的YAML文件 .Compose 文件的默认路径是 ./docker-compose.yml 提 ...

  6. 如何在ASP.NET MVC为Action定义筛选器

    在ASP.NET MVC中,经常会用到[Required]等特性,在MVC中,同样可以为Action自定义筛选器,来描述控制器所遵守的规则. 首先,我们在ASP.NET MVC项目中定义一个TestC ...

  7. 简单的发红包的PHP算法

    假设有有10元钱 ,发给10个人.保证每个人都有钱拿,最少分得0.01.我们最先想到的肯定就是随机.0.01-10随机.但是会出现第一个人就分得9.99的情况.下面就没人可分了.然后就是我的错误思路 ...

  8. F题

    Problem F Codeforces 16E 这道题是一道数位Dp将鱼的死活列为0两种状态然后找DP关系 •题意:有n(n<=18)条鱼,接下来的n-1天,每天会有一对鱼(a,b)相遇,每天 ...

  9. 565. Array Nesting

    Problem statement: A zero-indexed array A consisting of N different integers is given. The array con ...

  10. hdu 2795线段树

    #include<stdio.h> #define N 200005 int h,w,n; struct node { int x,y,max; }a]; int mmax(int e,i ...