码迷,mamicode.com
首页 > 编程语言 > 详细

python 地址簿

时间:2014-05-15 23:44:49      阅读:462      评论:0      收藏:0      [点我收藏+]

标签:style   class   code   c   int   a   

创建你自己的命令行 地址簿 程序。在这个程序中,你可以添加、修改、删除和搜索你的联系人(朋友、家人和同事等等)以及它们的信息(诸如电子邮件地址和/或电话号码)

#!/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()



python 地址簿,布布扣,bubuko.com

python 地址簿

标签:style   class   code   c   int   a   

原文地址:http://blog.csdn.net/u011345136/article/details/25916399

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有
迷上了代码!