清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
#!/usr/bin/python # -*- coding: utf-8 -*- import re class PhoneBook(object): '''这是一个电话簿脚本。 该脚本能够实现 AddContact:添加联系人信息 ShowContact:查找姓名显示联系人 SaveContacts:存储联系人到 TXT 文档(存储格式——姓名:号码/号码) LoadContacts:从 txt 文档中载入联系人 ''' def __init__(self): self.contactsDict = {} def AddContact(self): while True: name = raw_input('请输入姓名>>>') name = name.strip() # 姓名必须包含有效字符 if name != '': break print '***姓名不能为空' while True: number = raw_input('请输入号码>>>') number = re.sub(r'\D', '', number) # 删除号码中的非数字字符 if number != '': break print '***号码只能是数字' cover = True #若联系人已存在,是否覆盖 if self.contactsDict.has_key(name): print '***联系人已存在' self.ShowContact(name) while True: control = raw_input(''' 输入 "c":覆盖原来号码 输入 "n":保留原来号码并存储新号码 输入 "q" 退出\n>>>''') if control.lower() == 'n': cover = False break if control.lower() == 'c': break if control.lower() == 'q': return None print '***输入错误' if cover: self.contactsDict[name] = number else: if number in self.contactsDict[name]: print '***号码已经存在' else: self.contactsDict[name] = self.contactsDict[name] + '/' + number def ShowContact(self, name): print '++++++++++++++++++++++++' if self.contactsDict.has_key(name): print '【联系人信息】' print '【姓名: %s】' % name numberList = self.contactsDict[name].split('/') for num in range(len(numberList)): print '【号码%d: %s】' % (num+1, numberList[num]) else: print '【找不到联系人%s】' % name print '++++++++++++++++++++++++' def DeleteNumber(self, name): if self.contactsDict.has_key(name): self.ShowContact(name) number = self.contactsDict[name].split('/') while True: print ''' 请输入要删除号码的序号 或者输入 "a" 删除该联系人 或者输入 "q" 退出(不删除) (若联系人号码被全部删除,该联系人也将被删除)''' control = raw_input('>>>') if control.lower() == 'q': break elif control.lower() == 'a': del self.contactsDict[name] break elif control.isdigit() and int(control) <= len(number): del number[int(control)-1] self.contactsDict[name] = '/'.join(number) break else: print '***输入有误' def LoadContacts(self): ''' try: PhoneBook = open('PhoneBook.txt', 'a+') contacts = PhoneBook.read() if contacts == '': print '***电话簿为空' else: ContactsList = contacts.split('\n') for contact in ContactsList: if not contact == '': contact = contact.split(':') name = contact[0] number = contact[1] self.contactsDict[name] = number finally: PhoneBook.close() ''' self.contactsDict = {line.split(':')[0]: line.split(':')[1] for line in open('PhoneBook.txt','a+').readlines()} def SaveContacts(self): try: if self.contactsDict: PhoneBook = open('PhoneBook.txt', 'w') for name, number in self.contactsDict.items(): line = name + ':' + number PhoneBook.write(line) PhoneBook.write('\n') else: print '***没有联系人信息' finally: PhoneBook.close() if __name__ == '__main__': myPhoneBook = PhoneBook() myPhoneBook.LoadContacts() try: while True: raw_input('按回车键继续') print ''' -------------------------------- 输入 a:添加联系人 输入 s:显示联系人信息 输入 d:删除联系人 输入 q:退出 --------------------------------''' control = raw_input('>>>') if control.lower() == 'a': myPhoneBook.AddContact() elif control.lower() == 's': name = raw_input('请输入要查找的联系人姓名\n>>>') myPhoneBook.ShowContact(name) elif control.lower() == 'd': name = raw_input('请输入要删除的联系人姓名\n>>>') myPhoneBook.DeleteNumber(name) elif control.lower() == 'q': break else: print '***输入有误' finally: myPhoneBook.SaveContacts()