首页 > 代码库 > Python初学练习02:简易通讯录-优化搜索功能
Python初学练习02:简易通讯录-优化搜索功能
#!/usr/bin/env python
import tab,os,sys
exitcheck = False
listfile = ‘Addresslist.data‘
Dictionary = {}
#with open(listfile,‘a‘) as datafile
datafile=file(listfile,‘r‘)
datafile.seek(0)
for line in datafile.readlines():
id=line.split()[0]
name=line.split()[1]
phone=line.split()[2]
email=line.split()[3]
Dictionary[id]=(name,phone,email)
#print Dictionary
while exitcheck is False:
print "Welcome AddressList!"
print " 1#Display ALL List"
print " 2#Find Item"
print " 3#Addend Item"
print " 4#Dell Item"
print " 5#exit"
InputOption=int(raw_input("Please Input option[1-5]:").strip())
if InputOption == 1:
print "1#Display ALL List"
while InputOption != "q":
page=0
for key in Dictionary.keys():
print key,Dictionary[key][0],Dictionary[key][1],Dictionary[key][2]
if page == 4 :
page=0
InputOption = raw_input("q:break anyone:next").strip()
if InputOption == "q": break
else:page=page+1
if InputOption != "q":
InputOption = raw_input("q:break anyone:restart").strip()
elif InputOption == 2:
while InputOption != "q":
search = raw_input("Input Search Key:[>3 char,q:break]").strip()
InputOption = search
searchcount=0
findkeyid = ‘Null‘
if len(search) == 0:
continue
for id in Dictionary.keys():
valus=‘ ‘.join(Dictionary[id])
if valus.count(search) !=0 :
p_str= valus[:valus.find(search)]
p_str=p_str+‘\033[31;1m‘+search+‘\033[0m‘
p_str=p_str+valus[valus.find(search)+len(search):]
print p_str
findkeyid = id
searchcount+=1
if findkeyid !=‘Null‘ and searchcount ==1:
InputOption = raw_input("Option[d:del. e:edit. anyone:break")
if InputOption == "d":
InputOption =raw_input("delete it yes or no?").strip()
if InputOption =="y":
del Dictionary[findkeyid]
print "Del success!"
InputOption = ‘‘
elif InputOption =="e":
#print findkeyidi
print findkeyid+Dictionary[findkeyid][0]+" "+Dictionary[findkeyid][1]+" "+Dictionary[findkeyid][2]
nkeyid=findkeyid
nname=raw_input("NewName:").strip()
nphone=raw_input("NewPhone:").strip()
nemail=raw_input("NewEmail:").strip()
if len(nkeyid)>0:
Dictionary[nkeyid]=(nname,nphone,nemail)
print "Item Edit success! "
#InputOption = "q"
elif findkeyid >0:
print "Find count:%s" % searchcount
else:
print "Not Find Item"
elif InputOption ==3:
while InputOption != "q":
print "3#Addend Item"
nkeyid=""
nname=""
nphone=""
nemail=""
while len(nkeyid) == 0 or len(nname) == 0 :
nkeyid=raw_input("ID:").strip()
nname=raw_input("Name:").strip()
nphone=raw_input("Phone:").strip()
nemail=raw_input("Email:").strip()
if len(nkeyid) > 0 and len(nname) > 0 :
for keyid in Dictionary.keys():
if keyid == nkeyid :
print "ID is Repeat."
nkeyid=""
break
if len(nkeyid)>0:
Dictionary[nkeyid]=(nname,nphone,nemail)
print "Item Add success! "
InputOption = "q"
else:
print "ID or name is Null."
elif InputOption ==4:
while InputOption != "q":
print "4#Dell Item"
delkeyid=raw_input("DELETE ID:").strip()
if len(delkeyid) > 0:
del Dictionary[delkeyid]
print "Del success!"
InputOption = ‘q‘
elif InputOption ==5:
print "5#Exit"
break
savefile = file(listfile,‘w‘)
for key in Dictionary.keys():
str = key + ‘ ‘ + Dictionary[key][0] + ‘ ‘ + Dictionary[key][1] + ‘ ‘ + Dictionary[key][2] + ‘\n‘
savefile.write(str)
savefile.close
本文出自 “云横秦岭家何在” 博客,请务必保留此出处http://loren2012.blog.51cto.com/5089992/1869543
Python初学练习02:简易通讯录-优化搜索功能