zoukankan      html  css  js  c++  java
  • 员工信息表代码练习

    题目
    实现员工信息表
    文件存储格式如下:
    id,name,age,phone,job
    1,Alex,22,13651054608,IT
    2,Egon,23,13304320533,Tearcher
    3,nezha,25,1333235322,IT

    现在需要对这个员工信息文件进行增删改查。

    不允许一次性将文件中的行都读入内存。
    基础必做:
    a.可以进行查询,支持三种语法:
    select 列名1,列名2,… where 列名条件
    支持:大于小于等于,还要支持模糊查找。
    示例:
    select name, age where age>22
    select * where job=IT
    select * where phone like 133

    进阶选做:
    b.可创建新员工记录,id要顺序增加
    c.可删除指定员工记录,直接输入员工id即可
    d.修改员工信息
    语法:set 列名=“新的值” where 条件
    #先用where查找对应人的信息,再使用set来修改列名对应的值为“新的值”

    注意:要想操作员工信息表,必须先登录,登陆认证需要用装饰器完成
    其他需求尽量用函数实现

    我的代码
    自学了7天的一点小成果,没有找到作业讲解,所以花了半天的时间磕磕绊绊从头写到尾,不清楚实际作业的完成度是什么样子的,大家看个热闹了.
    另外进阶阶段还在弄,第一次写这么多代码,留个纪念哈哈!
    另外祝自己加油,继续好好学习!

    import re

    def judge(s):

        if '>' in s:
            s1 = s.split('>')
            if s1[0] > s1[1]:
                return True
            else:
                return False

        if '<' in s:
            s1 = s.split('<')
            if s1[0] < s1[1]:
                return True
            else:
                return False

        if '=' in s:
            s1 = s.split('=')
            if s1[0] == s1[1]:
                return True
            else:
                return False


    def select(info, condition):
        # print(info)
        # print(condition)
        with open('员工信息', encoding='utf-8') as f:
            for line in f:
                list = []
                line1 = line.split(',')
                if 'age' in condition:
                    c1 = condition.replace('age',line1[2])
                    if judge(c1):
                        if info == '*':
                            list = line1
                        if 'id' in info:
                            list.append(line1[0])
                        if 'name' in info:
                            list.append(line1[1])
                        if 'age' in info:
                            list.append(line1[2])
                        if 'phone' in info:
                            list.append(line1[3])
                        if 'job' in info:
                            list.append(line1[4].rstrip(' '))
                        print(' '.join(list).rstrip())

                if 'job' in condition:
                    c1 = condition.replace('job', line1[4].rstrip(' '))
                    if judge(c1):
                        if info == '*':
                            list = line1
                        if 'id' in info:
                            list.append(line1[0])
                        if 'name' in info:
                            list.append(line1[1])
                        if 'age' in info:
                            list.append(line1[2])
                        if 'phone' in info:
                            list.append(line1[3])
                        if 'job' in info:
                            list.append(line1[4].rstrip(' '))
                        print(' '.join(list).rstrip())

                if 'phone' in condition:
                    phone_number = re.sub('D', '', condition)
                    if phone_number in line1[3]:
                        if info == '*':
                            list = line1
                        if 'id' in info:
                            list.append(line1[0])
                        if 'name' in info:
                            list.append(line1[1])
                        if 'age' in info:
                            list.append(line1[2])
                        if 'phone' in info:
                            list.append(line1[3])
                        if 'job' in info:
                            list.append(line1[4].rstrip(' '))
                        print(' '.join(list).rstrip())
        return


    while 1:
        content = input('>>>')
        content = content.strip(' ')  #去掉前后多余空格
        if 'quit' in content:       #quit退出
            break
        elif content.startswith('select') == 0 or 'where' not in content:
            print('输入有误')       #开头必须是select,且文中要有where
            continue
        print('输入内容:%s'%content)

        '''
        下面要取出select和where之间内容
        '''
        info = content[6:content.find('where')].strip(' ')
        print('要执行的内容:{}'.format(info))

        '''
        下面要提取出where之后的信息
        '''
        condition = content[content.find('where')+5:].strip(' ')
        print('要执行的方法:%s'%condition)

        select(info, condition)

  • 相关阅读:
    文件上传漏洞总结篇
    python 构造mysql爆破器
    python写exploit采集器
    文件包含漏洞总结
    python Flask篇(一)
    Python写一个目录检索器
    python爬搜狗微信获取指定微信公众号的文章
    python打造文件包含漏洞检测工具
    python打造漏洞补丁缺少检测
    表单
  • 原文地址:https://www.cnblogs.com/mys6/p/10587104.html
Copyright © 2011-2022 走看看