{"id":1059,"date":"2009-05-18T23:37:28","date_gmt":"2009-05-18T15:37:28","guid":{"rendered":"http:\/\/fanhaijun.com\/?p=1059"},"modified":"2009-05-19T23:19:26","modified_gmt":"2009-05-19T15:19:26","slug":"python%e7%a8%8b%e5%ba%8f%e7%bb%83%e4%b9%a0-%e7%ae%80%e5%8d%95%e7%94%b5%e8%af%9d%e8%96%84","status":"publish","type":"post","link":"https:\/\/fanhaijun.com\/?p=1059","title":{"rendered":"python\u7a0b\u5e8f\u7ec3\u4e60&#8211;\u7b80\u5355\u7535\u8bdd\u8584"},"content":{"rendered":"<p><a href=\"http:\/\/fanhaijun.com\/wp-content\/uploads\/2009\/05\/pb.py\">python\u7a0b\u5e8f\u7ec3\u4e60&#8211;\u7b80\u5355\u7535\u8bdd\u8584\u4ee3\u7801<\/a><\/p>\n<pre lang=\"csharp\">\n#python\u7a0b\u5e8f\u7ec3\u4e60\u2013\u7b80\u5355\u7535\u8bdd\u8584 \n#\u6a0a\u6d77\u519b \n#fanhaijun.com \n#Python 2.6.2 \n\nimport cPickle as p \nimport string \nclass phonebook: \n        def __init__(self): \n                pblist=[] \n\ndef pb_add(): \n        #print \"This is the ADD function\" \n        while 1: \n                print \u2018\\nPlease input a NEW name[\"Q\" to back to main menu]:\\n\u2019 \n                add_name=raw_input(\"Name:\") \n\n                if add_name==\"Q\": \n                        main() \n                else: \n                        add_sex=raw_input(\"Sex:\") \n                        add_tel=raw_input(\"Tel:\")                       \n                pbdict={\"Name\":add_name,\"Sex\":add_sex,\"Tel\":add_tel}            \n                print \"You just input:\" \n                for i in pbdict.items(): \n                        print (\"%s:%-5s  \") % (i[0],i[1]), \n\n                pb.pblist.append(pbdict) \n                del pbdict \n             print (\"\\nRecord added sucessfully! Continue?(Y\/N)\\n\")              \n        while 1: \n                       con=raw_input() \n            if con==\"Y\": \n                            pb_add() \n                    elif  con==\"N\": \n                            pb_save() \n                            main() \n            else: \n                print (\"\\nError! Continue?(Y\/N)\\n\") \n\ndef pb_browser(): \n        #print \"This is the BROWSER function\" \n        #print pb.pblist \n\n        print \"\\nThere are %s records:\\n\"% len(pb.pblist) \n        print \"*******************************************\" \n        print \"  ID     NAME     SEX      TEL\" \n        print \"*******************************************\" \n    for i in pb.pblist: \n               #print \"===========================================\" \n                print \"%3s     \" % (pb.pblist.index(i)+1), \n                print \"%-8s\" % i[\"Name\"], \n                print \"%-8s\" % i[\"Sex\"], \n                print \"%-8s\" % i[\"Tel\"] \n            print \"===========================================\" \n        main()  \n\ndef pb_del(): \n        #print \"This is the DEL function\" \n\n        while 1: \n                print \"\\nPlease input the ID of the record you want to delete:[Input 0 to main menu]\\n\" \n                del_id=raw_input() \n                if int(del_id)==0: \n                        main() \n                elif del_id==\"\" or int(del_id)<0 or int(del_id)>len(pb.pblist): \n                        #print \"\\nPlease input a RIGHT ID :\\n\"   \n                        continue \n\n                elif int(del_id)<=len(pb.pblist) and int(del_id)>0: \n                        del pb.pblist[int(del_id)-1] \n            pb_save() \n\ndef pb_search(): \n        #print \"This is the SEARCH function\" \n\n    while 1: \n        print \"Please input what ever you want to search:\" \n        s=str(raw_input()).lower() \n        k=0 \n        id_list=[] \n        for i in pb.pblist: \n            if s in i[\"Name\"].lower() or s in i[\"Sex\"].lower()  or s in i[\"Tel\"].lower(): \n                k+=1 \n                id_list.append(pb.pblist.index(i)) \n        print \"\\nThere are %s results:\\n\"% k \n        #print id_list \n\n            print \"*******************************************\" \n        print \"  ID     NAME     SEX      TEL\" \n        print \"*******************************************\" \n        for j in id_list: \n            print \"%3s     \" % str(j+1), \n            print \"%-8s\" % pb.pblist[j][\"Name\"], \n                    print \"%-8s\" % pb.pblist[j][\"Sex\"], \n            print \"%-8s\" % pb.pblist[j][\"Tel\"] \n                print \"===========================================\"        \n\n                print (\"\\nContinue?(Y\/N)\\n\") \n        while 1: \n                       con=raw_input() \n            if con==\"Y\": \n                            pb_search() \n                    elif  con==\"N\": \n                            main() \n            else: \n                print (\"\\nError! Continue?(Y\/N)\\n\")\n\ndef pb_file(): \n        #print \"This is the file function\" \n        try: \n                f=file(pbfile) \n                pb.pblist=p.load(f) \n        except: \n                pb.list=[] \n        finally: \n                f.close() \n\ndef pb_save(): \n        #print \"This is the SAVE function\" \n        try: \n                f=file(pbfile,\"w\") \n                p.dump(pb.pblist,f) \n                print \"\\nRecords saved successfully!\\n\" \n        finally: \n                f.close() \n\ndef pb_quit(): \n        print \"Thanks for your using!\" \n        exit() \n\ndef main(): \n        print \"\"\"\\ \n____________________________________________________  \n|       Please select your choice:                 | \n|__________________________________________________| \n|       [A]: Add a record.                         | \n|       [B]: Browser the records.                  | \n|       [D]: Delete a record.                      | \n|       [S]: Search.                               | \n|       [Q]: Quit.                                 | \n|__________________________________________________| \n        \\ \n        \"\"\" \n        while 1: \n                opp=raw_input() \n                if opp==\"A\": \n                        pb_add() \n                if opp==\"B\": \n                        pb_browser() \n                if opp==\"D\": \n                        pb_del() \n                if opp==\"S\": \n                        pb_search() \n                if opp==\"Q\": \n                        pb_quit() \n\npb=phonebook() \npbfile=(\"pb.dat\") \npb_file() \n\nmain()\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>python\u7a0b\u5e8f\u7ec3\u4e60&#8211;\u7b80\u5355\u7535\u8bdd\u8584\u4ee3\u7801 #pyth&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[104],"tags":[193,196],"class_list":["post-1059","post","type-post","status-publish","format-standard","hentry","category-technogy","tag-python","tag-196"],"_links":{"self":[{"href":"https:\/\/fanhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/1059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fanhaijun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fanhaijun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fanhaijun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fanhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1059"}],"version-history":[{"count":0,"href":"https:\/\/fanhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/1059\/revisions"}],"wp:attachment":[{"href":"https:\/\/fanhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fanhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fanhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}