话不多说,直接上代码,比较神奇的事目前插入 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日 北京第一场雪,很美