学员信息表

作业:

有以下员工信息表

当然此表你在文件存储时可以这样表示


1

1,Alex Li,22,13651054608,IT,2013-04-01

现需要对这个员工信息文件,实现增删改查操作

  1. 可进行模糊查询,语法至少支持下面3种:

    1.   select name,age from staff_table where age > 22
    2.   select  * from staff_table where dept = "IT"
    3. select  * from staff_table where enroll_date like "2013"
    4. 查到的信息,打印后,最后面还要显示查到的条数
  2. 可创建新员工纪录,以phone做唯一键,staff_id需自增
  3. 可删除指定员工信息纪录,输入员工id,即可删除
  4. 可修改员工信息,语法如下:
    1.   UPDATE staff_table SET dept="Market" WHERE where dept = "IT"

 注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码!

下面是代码

#!/usr/bin/env python3# -*- coding: utf-8 -*-#user:Felix#date:2016-11-1#mail:[email protected]#If you have questions or suggestions, please contact the author.import osimport timeimport reimport shutildef data(data):    data=data.strip().replace(","," ").split(" ")    # if data[0] == "select":    #     print("select")    # elif data[0] == "update":    #     print("update")    # elif data[0] == "insert":    #     print("insert")    # elif data[0] == "delete":    #     print("delete")    # else:    #     print("输入语法有问题")    tag=False    for i in student_list[0]:        if i in data:            column.append(i)    for i in data:        if i == "where":            tag=True            continue        if tag:            condition.append(i)    return column,condition

def select(data):    select_list=[]    data=data.replace("select","").replace("from","").replace("staff_table where","")    #print(data) # *   age <= 22    mark=student_list[0].index(if_information[1][0]) #select name,age from staff_table where occupation like I    for i in student_list:        if i == student_list[0]:continue #如果是列名就跳出        #print("i[mark]  ",i[mark],"if_information[1][2]  ",if_information[1][2],"***",type(i[mark]),"  ",type(if_information[1][2]))        #if i[mark].isdigit():i[mark]=int(i[mark])        #if if_information[1][2].isdigit():if_information[1][2]=int(if_information[1][2])        if if_information[1][2].isdigit():            if if_information[1][1] == ">":                if int(i[mark]) > int(if_information[1][2]):select_list.append(i)#print(i)            elif if_information[1][1] == "=":                if int(i[mark]) == int(if_information[1][2]):select_list.append(i)#print(i)            elif if_information[1][1] == "<":                if int(i[mark]) < int(if_information[1][2]):select_list.append(i)#print(i)            elif if_information[1][1] == ">=":                if int(i[mark]) >= int(if_information[1][2]):select_list.append(i)#print(i)            elif if_information[1][1] == "<=":                if int(i[mark]) <= int(if_information[1][2]):select_list.append(i)#print(i)            elif if_information[1][1] == "!=":                if int(i[mark]) != int(if_information[1][2]):select_list.append(i)#print(i)            elif if_information[1][1] == "like":                if if_information[1][2] in i[mark]:select_list.append(i)#print(i)            else:print("您的输入有错")        #以上是 age和id int类型查询        else:            #print(i[mark],if_information[1][1],if_information[1][2])            if if_information[1][1] == "=":                if i[mark] == if_information[1][2]:select_list.append(i)#print(i)            elif if_information[1][1] == "like":                if if_information[1][2] in i[mark]:select_list.append(i)#print(i)            else:print("您的输入有错")

#以上是字符串类型查询    print_selcet_list=if_information[0]    #print(print_selcet_list,if_information[0],student_list[0])    count=[]    if "*" in data:        print(student_list[0])        for line in select_list:            print(line)        print("您一空查询到\033[1;32;31m%s\033[0m条信息!"%(len(select_list)))    else:        for i in print_selcet_list:            mark=student_list[0].index(i)            count.append(int(mark))        #print(count)        print(print_selcet_list)        for line in select_list:            if len(count) == 0:print(line[count[0]],line[count[1]],line[count[2]],line[count[3]],line[count[4]],line[count[5]])            elif len(count) == 1:print(line[count[0]])            elif len(count) == 2:print(line[count[0]],line[count[1]])            elif len(count) == 3:print(line[count[0]],line[count[1]],line[count[2]])            elif len(count) == 4:print(line[count[0]],line[count[1]],line[count[2]],line[count[3]])            elif len(count) == 5:print(line[count[0]],line[count[1]],line[count[2]],line[count[3]],line[count[4]])            elif len(count) == 6:print(line[count[0]],line[count[1]],line[count[2]],line[count[3]],line[count[4]],line[count[5]])        print("您一空查询到\033[1;32;31m%s\033[0m条信息!"%(len(select_list)))        # for line in select_list:        #     for i in count:        #        #         print(line[int(i)])    return select_list

def update(data_update):    #UPDATE staff_table SET dept="Market" WHERE dept = "IT"    #update staff_table set dept=market where dept = it    # print(data_update)    # print(if_information)    data1=data(data_update)[1]    news_ids = []    for id in data1:        if id not in news_ids:            news_ids.append(id)    news_ids.insert(0,"*")    # print(type(news_ids))    # print(news_ids)    select_list=select("".join(news_ids))    awk=data_update.strip().replace(","," ").split(" ")[3:6]    select_list_id=student_list[0].index(awk[0])    with open("dbnew","w",encoding="utf-8") as write_file:        #print(",".join(student_list[0]))        print(student_list[0])        write_file.write(",".join(student_list[0])+"\n")        for i in range(len(select_list)):            student_list.remove(select_list[i])            select_list[i][select_list_id]=awk[2]            print(select_list[i])            string=",".join(select_list[i])            #print(string)            write_file.write(string+"\n")        for s in student_list:            if "staff_id" in s:continue            #print(s)            else:write_file.write(",".join(s)+"\n")        print("您已经修改\033[1;32;31m%s\033[0m条信息!"%(len(select_list)))

#现在已经查出来了 写入新文件并同时修改        #print(awk)    write_file.close()    dbbak="dbbak"+time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime(time.time()))    shutil.copyfile("db",dbbak)    os.remove("db")    os.renames("dbnew","db")

# with open("db","r",encoding="utf-8") as read_file,\    #     open("dbnew","w",encoding="utf-8") as write_file:    #     for r_line in read_file:    #         for w_line in write_file:    #             if

#for line in student_list:        #line

def insert(data):

data = re.split(‘\(|\)‘,data)[-2].replace(" ","")    data1= re.split(‘\(|\)‘,data)    #print(data1)    for line in student_list:        if line[3] in data:            print("新增用户手机号与老用户\033[1;32;31m%s\033[0m有冲突"%line)            break    else:        sum=[]        with open("db","r",encoding="utf-8") as read_file,\            open("dbnew","w",encoding="utf-8") as write_file:            for r_line in read_file:                if "staff_id" not in  r_line:sum.append(r_line.split(",")[0])                write_file.write(r_line)            #print(type(",".join(data)))            # print(str(int(sum[-1])+1))            id=int(sum[-1])+1            # print(type(id))            # print(type(repr(id)))            #write_file.write("\n"+repr(int(sum[-1])+1)+","+",".join(data))            new_line="\n"+repr(id)+","+data            write_file.write(new_line)        # read_file.close()        # write_file.close()        dbbak="dbbak"+time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime(time.time()))        shutil.copyfile("db",dbbak)        os.remove("db")        os.renames("dbnew","db")

def delete(data_del):    #DELETE FROM staff_table WHERE staff_id = 1    print(data_del)    delete=data(data_del)[1][3:6]    delete.insert(0,"*")    print(delete)    select_list=select("".join(delete))    awk=data(data_del)[1][3:6]    print(awk)    select_list_id=student_list[0].index(awk[0])    with open("dbnew","w",encoding="utf-8") as write_file:        #print(",".join(student_list[0]))        print(student_list[0])        write_file.write(",".join(student_list[0])+"\n")        for i in range(len(select_list)):            student_list.remove(select_list[i])            # select_list[i][select_list_id]=awk[2]            print(select_list[i])            # string=",".join(select_list[i])            # #print(string)            # write_file.write(string+"\n")        for s in student_list:            if "staff_id" in s:continue            #print(s)            else:write_file.write(",".join(s)+"\n")    print("您已经删除\033[1;32;31m%s\033[0m条信息!"%(len(select_list)))    write_file.close()    dbbak="dbbak"+time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime(time.time()))    shutil.copyfile("db",dbbak)    os.remove("db")    os.renames("dbnew","db")

if __name__ == "__main__":    student_list=[]    f=open("db","r",encoding="utf-8")    for line in f:        line=line.strip().split(",")        student_list.append(line)

#print(student_list)    # msg=‘‘‘    # 1:查询    # 2:添加    # 3:删除    # 4:修改    # 5:退出    # ‘‘‘    # menu_dic={    #     ‘1‘:select,    #     ‘2‘:insert,    #     ‘3‘:delete,    #     ‘4‘:update,    #     ‘5‘:"",    # }    while True:        column=[]        condition=[]        #print(msg)        choice=input("数据>>: ").strip().lower().replace("\"","").replace("\‘","").replace(";","")        #choice="""INSERT INTO `staff_table`.`staff` VALUES (‘刘耀‘, ‘20‘, ‘17777777777‘,‘运维‘,"2011-11-11");"""        #choice=choice.strip().lower().replace("\"","").replace("\‘","").replace(";","")        #print(choice)        str=choice        if_information=list(data(str)) #[[‘name‘, ‘age‘], [‘age‘, ‘>‘, ‘22‘]]        #print("if_information==",if_information)        choice_def=choice.strip().replace(","," ").split(" ")        f.close()        if choice_def[0] == "select":            select(str)        elif choice_def[0] == "update":            update(str)        elif choice_def[0] == "insert":            insert(str)        elif choice_def[0] == "delete":            delete(str)        else:            print("输入语法有问题")        # if len(choice) == 0 or choice not in menu_dic:continue        # if choice == "5":break        #str=input("数据>>: ").strip()        # str="select name,age from staff_table where age > 22"        # if_information=list(data(str))        # menu_dic[choice](str)        #print(if_information)        # select name,age from staff_table where age > 22        # select name,age from staff_table where dept like I
时间: 2024-08-25 19:29:10

学员信息表的相关文章

Python开发【第xxx篇】函数练习题-----员工信息表

文件存储格式如下: 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   #> < s

Python 基础 - Day 4 Assignment - 员工信息表程序

作业要求及初步思路 员工信息表程序,实现增删改查操作: ① 可进行模糊查询,语法至少支持下面3种: select name,age from staff_table where age > 22 select * from staff_table where dept = "IT" select * from staff_table where enroll_date like "2013"② 解决方案: sql语句的python解析问题,即将用户输入的sql

delphi SysErrorMessage 函数和系统错误信息表 good

在看 API 文档时, 我们经常见到 GetLastError; 它可以返回操作后系统给的提示. 但 GetLastError 返回的只是一个信息代码, 如何返回对应的具体信息呢? FormatMessage 可以, 但这个函数太复杂了; 可以用 SysErrorMessage 代替它. 举例: [Delphi] view plaincopyprint? <pre><pre class="delphi" name="code">var err

打印简单公司员工信息表

要求,输入name不为空,输入次数最多3次,3次后跳出程序: 知识点: raw_input str转int whil if elif else continue break for 导入模块 引用变量值 格式化输出 vim #!/usr/bin/env python import sys user_name = "carson" this_year = 2014 counter  = 0 while True:     if counter < 3:         name =

python-作业:员工信息表

程序可实现以下功能:1.查询,输入select name,age from staff_table where age > 22,查询到符合要求的信息: 输入select * from staff_table where dept = "IT",查询到符合要求的信息: 输入select * from staff_table where enroll_date like "2013",查询到符合要求的信息.2.创建,输入insert Mickle,22,1365

python练习_module02-1-员工信息表

python 操作 员工信息表 要求: 可进行模糊查询,语法至少支持下面3种: select name,age from staff_table where age > 22select * from staff_table where dept = "IT"select * from staff_table where enroll_date like "2013" 查到的信息,打印后,最后面还要显示查到的条数 可创建新员工纪录,以phone做唯一键,sta

L01-04:python查询员工信息表练习

#decoding=utf-8 '''编写可供用户查询的员工信息表! 1|用户认证    ID Name department phone    查询关键字:姓名''' import linecache input01=raw_input("pls write your name:") i=1 name=[] count = len(open('user.txt','rU').readlines()) while i<=count:         fline = linecac

使用文档对象在页面上创建学生信息表。 信息表包括学号、姓名、性别、电子邮件、联系电话、个人主页和联系地址, 信息表内容通过表单输入,提交前先使用正则表达式进行验证,联系地址不能超过20个字符, 每输入一名学生的信息,提交后,表格增加一行,表格不能被选择、复制。

<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title></title>    </head>    <!--        描述:使用文档对象在页面上创建学生信息表.        信息表包括学号.姓名.性别.电子邮件.联系电话.个人主页和联系地址,        信息表内容通过表单输入,提交前先使用

用对象型泛型和ArraysList写一个输入学员信息并展示

题目:录入学员信息并保存,当录入学员的编号为0时结束,展示出学员信息 //student类 public class Student { public int id; public String name; public int age; public Student() { super(); } public Student(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = a