简单 的 通讯录讲解

18
1 of 18 简简简简简简简简 简简简简 简简 2014. 5. 27 简简简简简简简 -Python 简简

Upload: justine-watson

Post on 13-Mar-2016

62 views

Category:

Documents


3 download

DESCRIPTION

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

TRANSCRIPT

Page 1: 简单 的 通讯录讲解

1 of 18

简单的通讯录讲解讲课教师:常姗

2014. 5. 27

计算机编程导论-Python 语言

Page 2: 简单 的 通讯录讲解

2 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

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

程序说明

Page 3: 简单 的 通讯录讲解

3 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

Mainmain()Detailspoint() Peopleshow() Searchsearch(name) Addadd() Deletedelete(name) Editchange() Exitexit()

结构图

Page 4: 简单 的 通讯录讲解

4 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

os 模块提供了用于操作文件和目录的函数: 获得当前路径: os.getcwd( )

>>> import os

>>> cwd=os.getcwd()

>>> print cwd

检查一个路径 ( 文件或目录 ) 是否存在: os.path.exists(‘C:/

…’)

>>> os.path.exists(“E:\ 东华大学 \ 讲课 \Python\Python”)

>>> True

os 模块

Page 5: 简单 的 通讯录讲解

5 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

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

pickle 模块 - 序列化与反序列化

Page 6: 简单 的 通讯录讲解

6 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

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 ,文件对象需要是二进制模式打开的。

Pickle.dump()

Page 7: 简单 的 通讯录讲解

7 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

pickle.load(file) : 从 file 中读取一个字符串,并将它重构为原来的

python 对象。 file: 支持 read() 和

readline() 。

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()

Page 8: 简单 的 通讯录讲解

8 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

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() 写入文件

文件操作

Page 9: 简单 的 通讯录讲解

9 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

import osimport pickle

if os.path.exists('.\person.data') == False: f = open('.\person.data', 'w') temp = {} pickle.dump(temp, f) f.close()else: pass

检查通讯录是否存在,不存在则创建

Page 10: 简单 的 通讯录讲解

10 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

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("**************")

point() 菜单

Page 11: 简单 的 通讯录讲解

11 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

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

main()

Page 12: 简单 的 通讯录讲解

12 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

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!")

返回所有键构成的列表

将电话号码保存为字符串

Page 13: 简单 的 通讯录讲解

13 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

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()

show() 显示联系人通信录为空

Page 14: 简单 的 通讯录讲解

14 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

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()

Page 15: 简单 的 通讯录讲解

15 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

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")

Page 16: 简单 的 通讯录讲解

16 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

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!")

Page 17: 简单 的 通讯录讲解

17 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

def exit(): exec("quit()")

exit() 退出

Page 18: 简单 的 通讯录讲解

18 of 18Room 234, No.1 Building Donghua University, Shanghai, ChinaPhone 021- 67792289

[email protected]

完整程序: tongxunlu.py