码迷,mamicode.com
首页 > 其他好文 > 详细

shelve模块理解

时间:2016-11-26 02:06:19      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:import   close   hang   str   finally   int   test   理解   pid   

import shelve
import sys
def store_person(db):
    pid = input("Enter  unique ID mnumber:")
    person = {}
    person[name] = input("Enter name:")
    person[age] = input("Enter age:")
    person[phone] = input("Enter phone number:")
    db[pid] = person

def lookup_perosn(db):
    pid = input("Enter ID number:")
    field = input("What would you like to know? (name,age,phone)")
    filed = field.strip().lower()
    print(field.capitalize() + :,db[pid][field])

def print_help():
    print("The available commons are:")
    print("store:Looks up a person from ID number")
    print("lookup:Looks up a person from ID number")
    print("quit:save changes and exit")
    print("?:Print this message")

def enter_command():
    cmd = 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_perosn(database)
            elif cmd == ?:
                print_help()
            elif cmd == quit:
                return
    finally:
        database.close()
if __name__ == __main__:
    main()

转载至:http://www.cnblogs.com/xiaoli2018/p/4423460.html

shelve模块理解

标签:import   close   hang   str   finally   int   test   理解   pid   

原文地址:http://www.cnblogs.com/uglyliu/p/6103192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!