首页 > 代码库 > python 地址簿
python 地址簿
创建你自己的命令行 地址簿 程序。在这个程序中,你可以添加、修改、删除和搜索你的联系人(朋友、家人和同事等等)以及它们的信息(诸如电子邮件地址和/或电话号码)
#!/usr/bin/python # Filename : var.py import cPickle as p import os import sys filename = 'contacts.data' class member: def __init__(self, name, address, tel): self.name = name self.address = address self.tel = tel def select(): f = file(filename) conlist = p.load(f) print conlist s = raw_input('Please enter the name which you want to select: ') if s in conlist: print s, ':', conlist[s] else: print "Don't find!" def update(): s = raw_input('Please input similar to hehe,haha@.cn,10086: ') s1 = s.split(',') temp = member(s1[0], s1[1], s1[2]) f = file(filename) conlist = p.load(f) conlist[temp.name] = temp.address+','+temp.tel f = file(filename, 'w') p.dump(conlist, f) f.close() del conlist #print again f = file(filename) conlist = p.load(f) for name, other in conlist.items(): print 'name is: %s, other is: %s' % (name, other) def delete(): f = file(filename) conlist = p.load(f) print conlist d = raw_input("Please input the person's name you want to delete: ") del conlist[d] print conlist f = file(filename, 'w') p.dump(conlist, f) f.close() del conlist def main(): while True: choice = raw_input('1.select\n2.update\n3.delete\n4.exit\n') if choice == '1': select() elif choice == '2': update() elif choice == '3': delete() elif choice == '4': sys.exit() else: print "Don't have this option, Please try again!" if os.path.exists('contacts.data'): main() else: f = file('contacts.data', 'w') conlist = {'hehe':'haha@cn,10086'} p.dump(conlist, f) f.close() del conlist main()
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。