首页 > 代码库 > 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()