本文主要是介绍shelve函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
# -*- coding: cp936 -*-
import sys,shelve
def main():database = shelve.open("d:\\test.dat")try:while True:cmd = getcmd()if cmd == "s":store(database)elif cmd == "c":search(database)elif cmd == "q":returnelse:print "请选择正确的命令"finally:database.close()
def store(db):#person={}#person["name"] = raw_input("输入名称:")#person["score"] = raw_input("输入得分:")pid = raw_input("输入学号:")name = raw_input("输入名称:")score = raw_input("输入得分:")db[pid] = {"name":name,"score":score} # person
def search(db):pid = raw_input("请输入要查询的学号:")print db[pid]["name"]def getcmd():print "'s'=store , 'c'=search , 'q'=quit"cmd = raw_input("请选择操作:")return cmd.strip().lower()
main()
这篇关于shelve函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!