Python学习笔记-Day17-员工信息大作业

def add(str1):   #Alex,22,13651054608,IT
    with open('index',encoding='utf-8',mode='r') as f2:
        index = int(f2.read())
    with open('staff_info',encoding='utf-8',mode='a') as f:
        f.write('%s,%s\n' % (str(index+1),str1))
        index += 1
    with open('index','w') as f3:
        f3.write(str(index))


def select(col,con):  #col = 'name,age'  con =  'age>22'
    dic = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'job': 4}
    col_list = col.split(',')  #['name', 'age']
    if '>' in con:
        col_name,value = con.split('>')   #['age', '22']
        with open('staff_info',encoding='utf-8',mode='r') as f1:
            for line in f1:  #  1,Alex,22,13651054608,IT
                line_lst = line.split(',')  #['1', 'Alex', '22', '13651054608', 'IT']
                if int(line_lst[dic[col_name]]) > int(value):
                    if col == "*":
                        print(line)
                    else:
                        for i in col_list:
                            print(line_lst[dic[i]],end=' ')
                        print()
    if '<' in con:
        con_lst = con.split('<')   #['age', '22']
        with open('staff_info',encoding='utf-8',mode='r') as f1:
            for line in f1:  #  1,Alex,22,13651054608,IT
                line_lst = line.split(',')  #['1', 'Alex', '22', '13651054608', 'IT']
                if int(line_lst[dic[con_lst[0]]]) < int(con_lst[1]):
                    if col == "*":
                        print(line)
                    else:
                        for i in col_list:
                            print(line_lst[dic[i]],end=' ')
                        print()
    if '=' in con:
        con_lst = con.split('=')   #['age', '22']
        with open('staff_info',encoding='utf-8',mode='r') as f1:
            for line in f1:  #  1,Alex,22,13651054608,IT
                line_lst = line.split(',')  #['1', 'Alex', '22', '13651054608', 'IT']
                if int(line_lst[dic[con_lst[0]]]) == int(con_lst[1]):
                    if col == "*":
                        print(line)
                    else:
                        for i in col_list:
                            print(line_lst[dic[i]],end=' ')
                        print()
    if 'like' in con:
        con_lst = con.split('like')  # ['age', '22']
        with open('staff_info', encoding='utf-8', mode='r') as f1:
            for line in f1:  # 1,Alex,22,13651054608,IT
                line_lst = line.split(',')  # ['1', 'Alex', '22', '13651054608', 'IT']
                if con_lst[1] in line_lst[dic[con_lst[0]]].upper():
                    if col == "*":
                        print(line)
                    else:
                        for i in col_list:
                            print(line_lst[dic[i]],end=' ')
                        print()
def delet(del_num):
    import os
    with open('staff_info', encoding='utf-8', mode='r') as f1,\
            open('staff_info_bak', encoding='utf-8', mode='a') as f2:
        for line in f1:  #1,Alex,22,13651054608,IT
            line_list = line.split(',')
            if int(line_list[0]) != int(del_num):
                f2.write(line)
            else:
                continue
    os.remove('staff_info')
    os.rename('staff_info_bak','staff_info')

def set(col,con):  #age=22,name=Egon
    dic = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'job': 4}
    col_list = col.split('=')  #['age, '22']
    if '>' in con:
        con_list = con.split('>')  # ['age', '20']
        import os
        with open('staff_info', encoding='utf-8')as f1, \
                open('staff_info_bak', encoding='utf-8', mode='w') as f2:
            for line in f1:
                line_list = line.split(',')  # ['1', 'Alex', '22', '13651054608', 'IT\n']
                if int(line_list[dic[con_list[0]]]) > int(con_list[1]):
                    # line_list[dic[col_list[0]]] = col_list[1]
                    line = line.replace(line_list[dic[col_list[0]]],col_list[1])
                    f2.write(line)
                else:
                    f2.write(line)
        os.remove('staff_info')
        os.rename('staff_info_bak','staff_info')
    if '<' in con:
        con_list = con.split('<')  # ['age', '20']
        import os
        with open('staff_info', encoding='utf-8')as f1, \
                open('staff_info_bak', encoding='utf-8', mode='w') as f2:
            for line in f1:
                line_list = line.split(',')  # ['1', 'Alex', '22', '13651054608', 'IT\n']
                if int(line_list[dic[con_list[0]]]) < int(con_list[1]):
                    # line_list[dic[col_list[0]]] = col_list[1]
                    line = line.replace(line_list[dic[col_list[0]]],col_list[1])
                    f2.write(line)
                else:
                    f2.write(line)
        os.remove('staff_info')
        os.rename('staff_info_bak','staff_info')
    if '=' in con:
        con_list = con.split('=')  # ['age', '20']
        import os
        with open('staff_info', encoding='utf-8')as f1, \
                open('staff_info_bak', encoding='utf-8', mode='w') as f2:
            for line in f1:
                line_list = line.split(',')  # ['1', 'Alex', '22', '13651054608', 'IT\n']
                if line_list[dic[con_list[0]]] == con_list[1]:
                    # line_list[dic[col_list[0]]] = col_list[1]
                    line = line.replace(line_list[dic[col_list[0]]],col_list[1])
                    f2.write(line)
                else:
                    f2.write(line)
        os.remove('staff_info')
        os.rename('staff_info_bak','staff_info')



while 1:
    choose = input('请输入你的命令>>>').strip()
    choose = choose.replace(' ','')
    # print(choose)
    dic = {'id':0,'name':1,'age':2,'phone':3,'job':4}
    if choose == 'q':
        break
    if 'add' in choose:
        str1 = choose.split('add')[1]
        add(str1)

    if 'select' in choose:
        str1 = choose.split('select')[1]  #name,agewhereage>22
        col,con = str1.replace(' ','').split('where') #name,age age>22
        # print(col,con)
        select(col,con)

    if 'del' in choose:
        del_num = choose.replace(' ','').split('del')[1]
        delet(del_num)

    if 'set' in choose:  #set 列名=“新的值” where 条件
        str1 = choose.split('set')[1]  # set age=22 where name=Egon
        col, con = str1.replace(' ', '').split('where')  # col:age=22,name=Egon
        # print(col,con)
        set(col,con)

精简代码后:

def add(sql):
    with open('index_1',encoding='utf-8') as f1:
        index = int(f1.read())
    with open('staff_info_1',encoding='utf-8',mode='a') as f2:
        line = '%s,%s\n' % (index+1,sql)
        f2.write(line)
        index += 1
    with open('index_1',encoding='utf-8',mode='w') as f3:
        f3.write(str(index))

def read_line():
    with open('staff_info_1',encoding='utf-8') as f1:
        for line in f1:
            line_lst = line.strip().split(',')
            yield line_lst

def filter(col,col_name,val,col_lst,condition):
    dic = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'job': 4}
    for line_lst in read_line():  # ['1', 'Alex', '22', '13651054608', 'IT']
        if eval(condition):
            if col == '*':
                print(','.join(line_lst))
            else:
                for i in col_lst:
                    print(line_lst[dic[i]], end=' ')
                print()

def select(sql):#name, age where age>22
    col,con = sql.split('where')  #name,age    age>22
    col_lst = col.split(',')  #['name', 'age']
    if '>' in con:
        col_name, val = con.split('>')  # age 22
        condition = 'int(line_lst[dic[col_name]]) > int(val)'
        filter(col, col_name, val, col_lst, condition)
    elif '<' in con:
        col_name, val = con.split('<')  # age 22
        condition = 'int(line_lst[dic[col_name]]) < int(val)'
        filter(col, col_name, val, col_lst, condition)
    elif '=' in con:
        col_name, val = con.split('=')  # age 22
        condition = 'line_lst[dic[col_name]] == val'
        filter(col, col_name, val, col_lst, condition)
    elif 'like' in con:
        col_name, val = con.split('like')  # age 22
        condition = 'val in line_lst[dic[col_name]]'
        filter(col, col_name, val, col_lst, condition)

def delet(del_name):
    dic = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'job': 4}
    import os
    with open('staff_info_1',encoding='utf-8') as f,\
        open('staff_info_1_bak',encoding='utf-8',mode='w') as f1:
        for line in f:
            line_lst = line.strip().split(',')
            if del_name in line_lst:
                pass
            else:
                line = ','.join(line_lst)+'\n'
                f1.write(line)
    os.remove('staff_info_1')
    os.rename('staff_info_1_bak','staff_info_1')

def filter_set(con,col_name,val,col_lst,condition):
    dic = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'job': 4}
    import os
    with open('staff_info_1', encoding='utf-8') as f, \
            open('staff_info_1_bak', encoding='utf-8', mode='w') as f1:
        for line in f:
            line_lst = line.strip().split(',')
            if eval(condition):
                line = line.replace(line_lst[dic[col_lst[0]]], col_lst[1])
                f1.write(line)
            else:
                f1.write(line)
    os.remove('staff_info_1')
    os.rename('staff_info_1_bak', 'staff_info_1')

def set(sql):#列名=“新的值” where 条件
    new,con = sql.strip().split('where')
    col_lst = new.split('=')
    if '>'in con:
        col_name,val = con.split('>')
        condition = 'int(line_lst[dic[col_name]]) > int(val)'
        filter_set(con, col_name, val, col_lst, condition)
    if '<'in con:
        col_name,val = con.split('<')
        condition = 'int(line_lst[dic[col_name]]) < int(val)'
        filter_set(con, col_name, val, col_lst, condition)
    if '='in con:
        col_name,val = con.split('=')
        condition = 'line_lst[dic[col_name]] == val'
        filter_set(con, col_name, val, col_lst, condition)


while 1:
    sql = input('请输入指令>>>')
    if sql.lower() == 'q':
        break
    elif sql.startswith('add'):
        sql = sql.replace(' ','').split('add')[1]
        add(sql)
    elif sql.startswith('select'):
        sql = sql.replace(' ', '').split('select')[1]   #name, age where age>22
        select(sql)
    elif sql.startswith('del'):
        del_name = sql.replace(' ', '').split('del')[1]
        delet(del_name)
    elif sql.startswith('set'):
        sql = sql.replace(' ', '').split('set')[1]   #列名=“新的值” where 条件
        set(sql)

 

posted @ 2018-08-28 19:36  whuitian  阅读(245)  评论(0编辑  收藏  举报