python shelve

话不多说,直接上代码,比较神奇的事目前插入 NO 1\3的话,检索时候使用2检索一样能检索到3的内容

#shelve test as database

import shelve, sys

def db_insert(db):
    pid = input('Input a unique number:')
    person = {}
    person['name'] = input('input the name:')
    person['age']  = input('input the age:')
    person['phone'] = input('input the phone:')
    db[pid] = person
    return


def db_lookup(db):
    pid = input('The ID you want:')
    field = input('the field you want:(name, age, phone):')
    field = field.strip().lower()
    try:
        print('the info you want is :', \
          db[pid][field])
        return
    except KeyError:
        print('key not found')
        return


def enter_command():
    cmd = input('Enter command (insert|lookup|quit|? ? for help):')
    cmd = cmd.strip().lower()
    return cmd

def db_help():
    print('There are 3 commands:')
    print('insert: insert a person with id,name,age,phone')
    print('lookup: find a person with infomation')
    print('quit: save changes and exit')
    print('? : get the messages above')
    return


def main():
    print('hello this is a shelve test')
    database = shelve.open('shelve.bat')
    try:
        while True:
            cmd = enter_command()
            if cmd == '?':
                db_help()
            if cmd == 'insert':
                db_insert(database)
            if cmd == 'lookup':
                db_lookup(database)
            if cmd == 'quit':
                return
    finally:
        database.close()

if __name__ == '__main__':
    main()

  • 曾经还问过现在完全看不懂的问题呢

谢谢博主回复,已经完全看不懂自己在问什么了

2016年11月21日 北京第一场雪,很美

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值