题目:

一个文本通讯录要求:

  1. 内容含有:姓名,电话(手机,座机),qq,email
  2. 存储功能实现:通过命令行输入后保存到文件
  3. 读取功能和兼容性:自己程序能读出其他同学创建的通讯录
答案:

#!/usr/bin/env python
#!encoding:utf-8
#filename:contacts_list.py
#QUESTION FROM:http://uliweb.clkg.org/tutorial/view_chapter/183

import pickle, pprint
import os, sys
   
def create_contact(file_path): #初始化通讯录文本
    exist = os.path.isfile(file_path)
    if exist:
        os.remove(file_path)#如果已经存在则删除通讯录,下一步重新建立
        print "remove old conntact list successfully ! "
    init_contact = {
        'lijyt':{'cellphone':'13990000343','tel':'0512-86565544','qq':'45453242','e-mail':'erew@126.com'},
        'zhanw':{'cellphone':'15992222343','tel':'0523-89565544','qq':'89545445','e-mail':'ujjy@126.com'},
        'dongf':{'cellphone':'18905555343','tel':'0545-84565544','qq':'12453242','e-mail':'zxzc@126.com'}
        }
    f = open(file_path,'w')
    pickle.dump(init_contact,f)     
    f.close()
    print "create new contact list successfully !" 
    
def load_contact(file_path):#通讯录加载函数
    exist = os.path.isfile(file_path)#检查文件是否存在,不存在则退出
    if not exist:
        print "The file %s is not exist! check again " % file_path
        exit
        
    f = open(file_path,'r')
    contact = pickle.load(f)
    pprint.pprint(contact)
    f.close()
    
def add_contact(file_path):#增加用户函数(是否可以先写增加的dict,再通过update更新到contact?)
    exist = os.path.isfile(file_path)#检查文件是否存在,不存在则退出
    if not exist:
        print "The file %s is not exist! check again " % file_path
        exit
        
    f = open(file_path,'a+')
    contact = pickle.load(f)#加载通讯录

    #打开通讯录文本后进行新的用户添加输入
    name = raw_input('>> name:' )
    contact[name] = {}
    
    cellphone = raw_input('>> cellphone:' )
    contact[name]['cellphone'] = cellphone
    
    qq = raw_input('>> qq:' )
    contact[name]['qq'] = qq
    
    tel = raw_input('>> tel:' )
    contact[name]['tel'] = tel
    
    e_mail = raw_input('>> e-mail:' )
    contact[name]['e-mail'] = e_mail
    f.close()
    #写入文本中
    f = open(file_path,'w+')
    pickle.dump(contact,f)#重写通讯录
    f.close()

def menu_choice():#选择查询还是增加用户信息等
    print " *******Welcome to use this python contact_list******* "
    usage =  '''
         FUNCTION YOU CAN USE:
        (1)SEARCH SOMEONE INFOR ON THE CONTACT,INPUT: "search"
        (2)ADDING SOMEONE INFOR ON THE CONTACT,INPUT: "adding"
        (3)READING SOMEONE'S CONTACT LIST? INPUT: "reading"
        (4)(!ATTENTION)RESET CONTACT_LIST OR INITIALIZE: "reset"
    '''
    print usage,'\n'
 
    try:
        choice = raw_input('input your choice refered before >>')
        
        if choice  == 'reading':#一对if else选择进行读取自己的还是别人的conact
            input_dir = raw_input('input someone contact list file path (eg: E:\python\list1.txt)\n >>>')
            path_others = r'%s' % input_dir
            load_contact(path_others)
        else:
            in_put = raw_input('input your contact list file path (eg: E:\python\list1.txt)\n >>>')
            path = r'%s' % in_put
            #path = r'E:\study\python\tmp\contact_list1.txt'
            if choice == 'search':
                load_contact(path)
            elif choice == 'adding':
                add_contact(path)
            elif choice == 'reset':
                create_contact(path)
            else:
                print '''
                retry to run the program!
                %s
            ''' % usage
    except:
        print "something wrong!"
       

while True:
    menu_choice()

实现情况:

根据题目的要求,基本功能应该都实现了,剩下的只是兼容性及容错性的问题,个人觉得可以进行改进的方面如下:

1)对dict的更新,可否用update去实现,并同步到文本?反复加载或者重写对大数据处理可能就变慢了

2)对于读取他人的通讯录,需要进行严格的限制,否则不符合pickle的格式,导致无法读取,有无其他更好的兼容方法?

3)有几个地方可以循环的更流畅,每次选择时,可否只输入一次目录地址,如果没有选择reading,都按默认选项来?这样可以方便反复增加用户,这个应该容易,循环嵌套问题。

4)查询时,可否按名字查询,或者其它选择方式?可以更加明确目标。

5)对某个用户进行删除,该如何做呢?

其它就是过程简化方面的了,暂时想到上面的想法。


下面运行起来看下结果如何:


输出:

>>> ================================ RESTART ================================
>>> 
 *******Welcome to use this python contact_list******* 

         FUNCTION YOU CAN USE:
        (1)SEARCH SOMEONE INFOR ON THE CONTACT,INPUT: "search"
        (2)ADDING SOMEONE INFOR ON THE CONTACT,INPUT: "adding"
        (3)READING SOMEONE'S CONTACT LIST? INPUT: "reading"
        (4)(!ATTENTION)RESET CONTACT_LIST OR INITIALIZE: "reset"
     

input your choice refered before >>search
input your contact list file path (eg: E:\python\list1.txt)
 >>>D:\test_tmp
The file D:\test_tmp is not exist! check again 
something wrong!
 *******Welcome to use this python contact_list******* 

         FUNCTION YOU CAN USE:
        (1)SEARCH SOMEONE INFOR ON THE CONTACT,INPUT: "search"
        (2)ADDING SOMEONE INFOR ON THE CONTACT,INPUT: "adding"
        (3)READING SOMEONE'S CONTACT LIST? INPUT: "reading"
        (4)(!ATTENTION)RESET CONTACT_LIST OR INITIALIZE: "reset"
     

input your choice refered before >>reset
input your contact list file path (eg: E:\python\list1.txt)
 >>>D:\test_tmp\contact_list.txt
create new contact list successfully !
 *******Welcome to use this python contact_list******* 

         FUNCTION YOU CAN USE:
        (1)SEARCH SOMEONE INFOR ON THE CONTACT,INPUT: "search"
        (2)ADDING SOMEONE INFOR ON THE CONTACT,INPUT: "adding"
        (3)READING SOMEONE'S CONTACT LIST? INPUT: "reading"
        (4)(!ATTENTION)RESET CONTACT_LIST OR INITIALIZE: "reset"
     

input your choice refered before >>search
input your contact list file path (eg: E:\python\list1.txt)
 >>>D:\test_tmp\contact_list.txt
{'dongf': {'cellphone': '18905555343',
           'e-mail': 'zxzc@126.com',
           'qq': '12453242',
           'tel': '0545-84565544'},
 'lijyt': {'cellphone': '13990000343',
           'e-mail': 'erew@126.com',
           'qq': '45453242',
           'tel': '0512-86565544'},
 'zhanw': {'cellphone': '15992222343',
           'e-mail': 'ujjy@126.com',
           'qq': '89545445',
           'tel': '0523-89565544'}}
 *******Welcome to use this python contact_list******* 

         FUNCTION YOU CAN USE:
        (1)SEARCH SOMEONE INFOR ON THE CONTACT,INPUT: "search"
        (2)ADDING SOMEONE INFOR ON THE CONTACT,INPUT: "adding"
        (3)READING SOMEONE'S CONTACT LIST? INPUT: "reading"
        (4)(!ATTENTION)RESET CONTACT_LIST OR INITIALIZE: "reset"
     

input your choice refered before >>adding
input your contact list file path (eg: E:\python\list1.txt)
 >>>D:\test_tmp\contact_list.txt
>> name:boys
>> cellphone:13487677868
>> qq:3454554655
>> tel:0513-65546567
>> e-mail:56ert@163.com
 *******Welcome to use this python contact_list******* 

         FUNCTION YOU CAN USE:
        (1)SEARCH SOMEONE INFOR ON THE CONTACT,INPUT: "search"
        (2)ADDING SOMEONE INFOR ON THE CONTACT,INPUT: "adding"
        (3)READING SOMEONE'S CONTACT LIST? INPUT: "reading"
        (4)(!ATTENTION)RESET CONTACT_LIST OR INITIALIZE: "reset"
     

input your choice refered before >>search
input your contact list file path (eg: E:\python\list1.txt)
 >>>D:\test_tmp\contact_list.txt
{'boys': {'cellphone': '13487677868',
          'e-mail': '56ert@163.com',
          'qq': '3454554655',
          'tel': '0513-65546567'},
 'dongf': {'cellphone': '18905555343',
           'e-mail': 'zxzc@126.com',
           'qq': '12453242',
           'tel': '0545-84565544'},
 'lijyt': {'cellphone': '13990000343',
           'e-mail': 'erew@126.com',
           'qq': '45453242',
           'tel': '0512-86565544'},
 'zhanw': {'cellphone': '15992222343',
           'e-mail': 'ujjy@126.com',
           'qq': '89545445',
           'tel': '0523-89565544'}}
 *******Welcome to use this python contact_list******* 

         FUNCTION YOU CAN USE:
        (1)SEARCH SOMEONE INFOR ON THE CONTACT,INPUT: "search"
        (2)ADDING SOMEONE INFOR ON THE CONTACT,INPUT: "adding"
        (3)READING SOMEONE'S CONTACT LIST? INPUT: "reading"
        (4)(!ATTENTION)RESET CONTACT_LIST OR INITIALIZE: "reset"
     

input your choice refered before >>reading
input someone contact list file path (eg: E:\python\list1.txt)
 >>>D:\test_tmp\contact_list.txt
{'boys': {'cellphone': '13487677868',
          'e-mail': '56ert@163.com',
          'qq': '3454554655',
          'tel': '0513-65546567'},
 'dongf': {'cellphone': '18905555343',
           'e-mail': 'zxzc@126.com',
           'qq': '12453242',
           'tel': '0545-84565544'},
 'lijyt': {'cellphone': '13990000343',
           'e-mail': 'erew@126.com',
           'qq': '45453242',
           'tel': '0512-86565544'},
 'zhanw': {'cellphone': '15992222343',
           'e-mail': 'ujjy@126.com',
           'qq': '89545445',
           'tel': '0523-89565544'}}
 *******Welcome to use this python contact_list******* 

         FUNCTION YOU CAN USE:
        (1)SEARCH SOMEONE INFOR ON THE CONTACT,INPUT: "search"
        (2)ADDING SOMEONE INFOR ON THE CONTACT,INPUT: "adding"
        (3)READING SOMEONE'S CONTACT LIST? INPUT: "reading"
        (4)(!ATTENTION)RESET CONTACT_LIST OR INITIALIZE: "reset"
     

input your choice refered before >>

上面执行的步骤是:

1、文件不存在

2、创建contact list

3、查询

4、增加用户

5、查询是否增加

6、读取其它通讯录(实际中用的是原来的)

整个流程基本是走完了,看样子还行。


总结:

这个程序中用到的知识:

1、文件的操作(打开,移除,创建)

2、pickle模块的使用,加载与生成

3、字典的初始化,增删

4、输入与字典赋值

5、循环的使用if else,while,异常处理等






posted on 2022-07-05 18:12  我在全球村  阅读(58)  评论(0编辑  收藏  举报