1 / 18

简单 的 通讯录讲解

计算机编程导论 - Python 语言. 简单 的 通讯录讲解. 讲课教师:常姗 2014. 5. 27. 程序 说明. 一个非 GUI 界面的简单通讯录,实现 以下功能: 增加 联系人 删除 联系人 编辑联系人 搜索联系人 显示联系人. 结构图. o s 模块. os 模块 提供 了用于操作 文件和目录的函数: 获得当前路径: os.getcwd ( ) >>> import os >>> cwd = os.getcwd () >>> print cwd

Download Presentation

简单 的 通讯录讲解

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 计算机编程导论 -Python语言 简单的通讯录讲解 讲课教师:常姗 2014. 5. 27

  2. 程序说明 • 一个非GUI界面的简单通讯录,实现以下功能: • 增加联系人 • 删除联系人 • 编辑联系人 • 搜索联系人 • 显示联系人

  3. 结构图

  4. os模块 • os模块提供了用于操作文件和目录的函数: • 获得当前路径:os.getcwd( ) >>> import os >>> cwd=os.getcwd() >>> print cwd • 检查一个路径(文件或目录)是否存在:os.path.exists(‘C:/…’) >>> os.path.exists(“E:\东华大学\讲课\Python\Python”) >>> True

  5. pickle模块-序列化与反序列化 • pickle模块实现了基本的数据序列和反序列化。 • 序列化操作:将程序中运行的对象信息保存到文件中去,永久存储; • 反序列化操作:从文件中创建上一次程序保存的对象。

  6. Pickle.dump() • pickle.dump(obj, file, [,protocol]) • 将对象obj保存到文件file中去: • protocol为序列化使用的协议版本 • 0:ASCII协议,所序列化的对象使用可打印的ASCII码表示; • 1:老式的二进制协议; • 2:2.3版本引入的新二进制协议,较以前的更高效。其中协议0和1兼容老版本的python。protocol默认值为0。 • file:对象保存到的文件。 • file必须支持write() • 如果protocol>=1,文件对象需要是二进制模式打开的。

  7. pickle.load() import pickle   class Person:   def __init__(self,n,a):           self.name=n   self.age=a   def show(self):           print self.name+"_"+str(self.age)   aa = Person("JGood", 2)   aa.show()   f=open('d:\\p.txt','w')   pickle.dump(aa,f,0)   f.close()   f=open('d:\\p.txt','r')   bb=pickle.load(f)   f.close()   bb.show() • pickle.load(file): • 从file中读取一个字符串,并将它重构为原来的python对象。 • file:支持read()和readline()。

  8. 文件操作 • r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件 • f = open('.\person.data', 'r') • a = pickle.load(f) • f.close() • 读取文件 • f = open('.\person.data', 'w') • pickle.dump(a, f) • f.close() • 写入文件

  9. 检查通讯录是否存在,不存在则创建 import os import pickle if os.path.exists('.\person.data') == False: f = open('.\person.data', 'w') temp = {} pickle.dump(temp, f) f.close() else: pass

  10. point() 菜单 def point(): print( "*************") print("detail: 0") print("people: 1") print("search: 2") print("add: 3") print("delete: 4") print("edit: 5") print("exit: 6") print("**************")

  11. main() def main(): point() while True: x = input("input your choice~\n") if x == 3: add() continue if x == 1: show() continue if x == 6: exit() continue if x == 2: name = raw_input("input the name you wanna search:") search(name) continue if x == 4: name = raw_input("input the name you wanna delete:") delete(name) continue if x == 5: change() continue if x == 0: point() else: print("the choice you input is not exit!") continue

  12. add() 增加联系人 def add(): f = open('.\person.data', 'r') a = pickle.load(f) f.close() name = raw_input("tell me the name you wanna add:") for key in a.keys(): if key == name: print("the person you wanna add is already here!") f.close() return number = input("tell me the number:") a[name]=str(number) f = open('.\person.data', 'w') pickle.dump(a, f) f.close() print("Worked!") 返回所有键构成的列表 将电话号码保存为字符串

  13. show() 显示联系人 def show(): f = open('.\person.data', 'r') a = pickle.load(f) print("there are/is totally %d person(s)." % (len(a))) if len(a)==0: print "the address list is empty!“ f.close() return for key in a.keys(): print key+":"+str(a[key]), print f.close() 通信录为空

  14. search(name) 搜索联系人 def search(name): f = open('.\person.data', 'r') a = pickle.load(f) for key in a.keys(): if key == name: print("%s:the number is: %s" % (key,a[key])) f.close() return print("the person you search is not here!") f.close()

  15. delete(name) 删除联系人 def delete(name): f = open('.\person.data', 'r') a = pickle.load(f) f.close() for key in a.keys(): if key == name: del a[key] f = open('.\person.data', 'w') pickle.dump(a, f) f.close() print("Deleated!") return print("the person is not here")

  16. change() 编辑联系人 def change(): x = raw_input("tell me the name who you wanna edit:") f = open('.\person.data', 'r') a = pickle.load(f) f.close() for key in a.keys(): if key == x: y = input("the number after edit:") a[key] = y f = open('.\person.data','w') pickle.dump(a, f) f.close() print("Worked!") return print("the person is not here!")

  17. exit() 退出 def exit(): exec("quit()")

  18. 完整程序:tongxunlu.py

More Related