Python初學練習02:簡易通訊錄-最佳化搜尋功能

來源:互聯網
上載者:User

標籤:python

#!/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:簡易通訊錄-最佳化搜尋功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.