Programming the file CRUD

import os
def fetch(data):
    print('\033[1;41m这是查询功能\033[0m')
    print('\033[1;41m用户数据\033[0m', data)
    backend_data = 'backend %s' %data
    with open ('lala.conf', 'r', encoding = 'utf-8') as read_f:
        tag = False
        ret = []

        for read_line in read_f:
            if read_line.strip() == backend_data:
                tag = True
                continue
            if tag and read_line.startswith('backend'):
                break
            if tag:

                ret.append(read_line)

    return ret

def add():
    pass

def change(data):
    print ( "This is a modification function")
    backend = data[0]['backend']   #文件中的一条记录 www.oldboy1.org
    backend_data = 'backend %s' %backend

    old_service_recode = '%sservice %s %s weight %s maxconn %s\n' % (' '*8,data[0]['record']['service'],
                                                                     data[0]['record']['service'],
                                                                     data[0]['record']['weight'],
                                                                     data[0]['record']['maxconn'])

    new_service_recode = '%sservice %s %s weight %s maxconn %s\n' % (' ' * 8, data[1]['record']['service'],
                                                                     data[1]['record']['service'],
                                                                     Data [. 1] [ 'Record'] [ 'weight'], 
                                                                     Data [. 1] [ 'Record'] [ 'The maxconn']) 
    Print ( 'the user wants to record is modified S%'% old_service_recode) 
    RES = FETCH ( backend) 
    IF or old_service_recode not not RES in RES: 
        return 'you want to edit the file does not exist' 
    the else: 
        index1, = res.index (old_service_recode) 
        RES [index1,] = new_service_recode 
    res.insert (0, '% S \ n-'% backend_data) 

    with Open ( 'lala.conf', 'R & lt', encoding = 'UTF8') AS read_f, Open ( 'lala_new.conf', 'W', encoding = 'UTF8') AS write_f:
        tag = False
        has_write = False
        for read_line in read_f:

            if read_line.strip() == backend_data:
                tag = True
                continue
            if tag and read_line.startswith('backend'):
                tag = False
            if not tag:
                write_f.write(read_line)
            else:
                if not has_write:
                    for record in res:
                        write_f.write(record)
                    has_write = True
    os.rename('lala.conf', 'sb')
    os.rename('lala_new.conf', 'lala.conf')
    os.remove('sb')







def delete():
    pass

if __name__ == '__main__':
    msg = '''
    1:Query 
    2: Add 
    . 3: Review 
    4: Delete 
    5: Exit 
    ' '' 
    msg_dic = { 
        '. 1': FETCH, 
        '2': the Add, 
        '. 3': Change, 
        '4': Delete 
    } 

    the while True: 
        Print (MSG ) 
        Choice = the iNPUT ( 'Please enter your options:'.) Strip () 
        IF not Choice: If the Continue # input is empty or enter, re-enter the 
        IF Choice == '5': BREAK 

        the Data = the iNPUT ( 'please enter your data: '.) Strip () 
        IF = Choice!'. 1 ': 
            data = the eval (data) 

        RES = msg_dic [Choice] (data) 
        Print (RES)

  数据输入:[{'backend':'www.oldboy1.org', 'record': {'service':'2.2.2.4','weight':20,'maxconn':30000}},{'backend':'www.oldboy1.org','record': {'service':'2.2.2.5','weight':30,'maxconn':4000}}]

Guess you like

Origin www.cnblogs.com/dabai123/p/11285822.html