shelve模块理解

Posted 梦轻尘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shelve模块理解相关的知识,希望对你有一定的参考价值。

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模块理解的主要内容,如果未能解决你的问题,请参考以下文章

python shelve 模块

Python标准库之shelve模块(序列化与反序列化)

shelve模块和xml模块

13.4 Shelve模块

Python模块-shelve模块

day5-shelve模块