shelve模块如何实现删除?
import shelve
def store_person(db):
'''
Query user for data and store it in the shelf object
'''
pid = raw_input('Enter your unique ID number:')
person = {}
person['name'] = raw_input('Enter your name:')
person['age'] = raw_input('Enter your age:')
person['phone'] = raw_input('Enter your phone number:')
db[pid] = person
def delete_person(db):
pid = raw_input('Enter your want delete ID number:')
如何通过得到pid从db中删除整条记录?
def print_help():
print 'The avaliable command are:'
print 'store : Stores informaiton about a person'
print 'del : Delete information about a person'
print 'quit : Save changes and exit'
print '? : Print this message'
def enter_command():
cmd = raw_input('Enter command(? for help):')
cmd = cmd.strip().lower()
return cmd
def main():
database = shelve.open('D:\\backup\\database.dat', 'c')
try:
while True:
cmd = enter_command()
if cmd == 'store':
store_person(database)
elif cmd == 'del':
delete_person(database)
elif cmd == '?':
print_help()
elif cmd == 'quit':
return
finally:
database.close()
if __name__ == '__main__':
main()
比如我添加了第一条记录pid为001,name为m,age为6,phone为1234567存储在database.dat中
然后我想用删除功能删除这条记录,位置如代码中的部分。
[解决办法]
windows下嘛,程序集菜单里点击python manuals...