简明python地址簿

最近看了一本叫做《简明Python》的书。

书中最后竟然有这么个小实例。

其中用到了python中的词典和Cpickle存储。还有面向对象编程的思想(os:让一个没有对象的人面向对象简直惨)


#!/usr/bin/python
#Filename:Contact_address.py

import cPickle as p
import os
import sys
filename='addressbook.data'

class Person:
	def __init__(self,name,PhoneNum,address):
		self.name=name
		self.PhoneNum=PhoneNum
		self.address=address

def update():
	content=raw_input('please input similar to leo,18636658660,leo@outlook.com>>\n')
	con=content.split(',')
	context=Person(con[0],con[1],con[2])
	f=file(filename)
	conlist=p.load(f)
	conlist[context.name]=context.PhoneNum+','+context.address
	f=file(filename,'w')
	p.dump(conlist,f)
	f.close()
	del conlist
	
#	f=file(filename)
#	conlist=p.load(f)
#	print conlist

def delete():
	f=file(filename)
	conlist=p.load(f)
	print conlist
	d=raw_input("please input the person's name you want to delete\n")
	del conlist[d]
	print conlist
	f=file(filename,'w')
	p.dump(conlist,f)
	f.close()
	del conlist

def select():
	f=file(filename)
	conlist=p.load(f)
#	print conlist
	s=raw_input("please input the person's name you want to select\n")
	if(s in conlist):
		print s,':',conlist[s]
	else:
		print '%s is not exist'%s 

def main():
	while True:
		menu=raw_input('''''
			1.select
			2.update
			3.delete
			x.exit
			------>\n''')
		if menu=='1':
			select()
		elif menu=='2':
			update()
		elif menu=='3':
			delete()
		elif menu=='x':
			sys.exit()
		else:
			print"do not have this option,please try again"
		
if os.path.exists('addressbook.data'):
	main()
else:
	f=file('addressbook.data','w')
	conlist={'leo':'18636658660,leo@outlook.com'}
	p.dump(conlist,f)
	f.close()
	del conlist
	main()

这大体就是这样

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值