周末大作业:实现员工信息表
文件存储格式如下:
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来修改列名对应的值为“新的值”

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

import os
name_list = ['id', 'name', 'age', 'phone', 'job'] #创建列名列表
ditons=['>','<','=','like']
user_dic={'name':'admin','passwd':''}
status={'name':None,'passwd':False,'login':False}
def auth(x):
def user_auth(*args,**kwargs):
if not status['login']:#获取user_dic字典里面的login值
username=input('请输入用户名')
userpasswd=input('请输入密码')
if username==user_dic['name'] and userpasswd==user_dic['passwd']:#判断账户密码是否正确
status['name']=username #如果正确修改user_dic里面的信息
status['passwd']=userpasswd
status['login']=True
print('登陆成功')
res=x(*args,**kwargs)
return res
else:
print('用户名或密码输入错误')
else:#如果login为True,直接执行函数
res=x(*args,**kwargs)
return res
return user_auth#函数闭包 def check():
s=input('请输入查询语句,例如 select * where age=25\n')
if 'select' and 'where'in s:#检查输入语句是否正确
content, condition = s.split('where') #以where分割s,并且赋值给content,condition
content = ''.join(content.split('select')[1:]) #content 以select分割,取最后一个赋值给content
if content.strip() and condition.strip(): #判断 如果content 和condition都不为空,执行下面语句
for key in ditons: #for循环遍历ditions,
if key in condition: #如果key 在condition里面的话,执行下列语句
index_condition = ditons.index(key)#获取key在ditons里面的索引
name, cond = condition.strip().split(key)#以key分割condition,赋值给name,cond
if name in name_list:#如果name在name_list里面,执行下列语句
with open('mysql', encoding='utf-8') as f: # r模式打开员工信息表
for line in f: # 逐行循环
if line.strip(): # 如果这一行不为空
line_list = line.strip().replace(',', ',').split(',')#将line以逗号分割以列表形式赋值给line_list,
if key == ">" and int(line_list[name_list.index(name)]) > int(cond): #如果key为> 且列表对应索引的值大于查询语句的值,那么就执行下面语句
if content.strip() == '*':#如果content为*,也就是全部打印
print(line.strip()) #打印line
else:
if ',' in content:#如果逗号在content里面,意味着打印多个,不打印全部
str1 = '' #定义一个空字符串
select_name_list = content.strip().split(',') #content 以逗号分割,以列表的形式赋值给select_name_list
select_name_list = [i for i in select_name_list if i != ''] #去掉select_name_list 的空字符串
for names in select_name_list:#for 循环select_name_list
names_index = name_list.index(names.strip())#找到关键字的索引
str1 = str1 + line_list[names_index] + ',' #赋值给str1
print(str1)#打印
else:
print(line_list[name_list.index(content.strip())])#如果不存在逗号,只打印单个,直接找到索引打印即可
if key == "<" and int(line_list[name_list.index(name)]) < int(cond):
if content.strip() == '*':
print(line.strip())
else:
if ',' in content:
str1 = ''
select_name_list = content.strip().split(',')
select_name_list = [i for i in select_name_list if i != '']
for names in select_name_list:
names_index = name_list.index(names.strip())
str1 = str1 + line_list[names_index] + ','
print(str1)
else:
print(line_list[name_list.index(content.strip())])
if key == "=" and line_list[name_list.index(name)] == cond:
if content.strip() == '*':
print(line.strip())
else:
if ',' in content:
str1 = ''
select_name_list = content.strip().split(',')
select_name_list = [i for i in select_name_list if i != '']
for names in select_name_list:
names_index = name_list.index(names.strip())
str1 = str1 + line_list[names_index] + ','
print(str1)
else:
print(line_list[name_list.index(content.strip())])
if key == 'like':
name = name.strip()
cond = cond.strip()
key = key.strip()
if cond in line_list[name_list.index(name)]:
if content.strip() == '*':
print(line.strip())
else:
if ',' in content:
str1 = ''
select_name_list = content.strip().split(',')
select_name_list = [i for i in select_name_list if i != '']
for names in select_name_list:
names_index = name_list.index(names.strip())
str1 = str1 + line_list[names_index] + ','
print(str1)
else:
print(line_list[name_list.index(content.strip())])
else:
print('查无关键字')
else:
print('语句格式错误')
else:
print('查询语句输入错误') def delete():
s=input('请输入要删除的ID,例如:delete * where id=1\n')
if 'delete' and 'where ' in s:
flag = False
delete_content,delete_cond=s.strip().split('where')#以where分割
if delete_content.strip() and delete_cond.strip():#判断delete_content和delete_cond是否都不为空,不为空执行下面语句
name, value = delete_cond.strip().split('=')#以=分割
with open('mysql', 'r', encoding='utf-8') as f:#打开文件
for line in f: #for循环文件内容
if line.strip():#如果内容不为空
line_list = line.strip().split(',')#以逗号分割,以列表的形式赋值给line_list
if line_list[name_list.index(name.strip())] == value.strip():#如果要更新的值和文件中对应索引的值匹配,执行下面语句
flag=True#flag改为True
if flag:#如果flag为True 执行下列语句
with open('mysql', 'r', encoding='utf-8') as f, open('mysql.bak', 'a', encoding='utf-8') as f1:#打开文件
for line in f:
if line.strip():
line_list = line.strip().split(',')
if line_list[name_list.index(name.strip())] != value.strip():#如果更新的值和文件中不匹配,就将内容写入新文件,匹配就不写入
f1.write(line)
if flag:#flag为True才会去删除文件,和重命名文件
os.remove('mysql')
os.rename('mysql.bak', 'mysql')
if not flag:#如果flag为True就不执行,为False就执行
print('找不到相应数据')
else:
print('语句格式错误')
else:
print('输入错误')
def add():
s=input('请输入要添加的信息,例如 add children,26,1501375,Student\n')
add_user=s.strip().split('add ')#以add+空格分割
user_str=''#定义空字符串
for i in add_user:#因为add_user是列表,所以用for循环转为字符串
user_str+=i
user_id=0#定义值,用于获取id
with open('mysql','r',encoding='utf-8') as f:#打开文件
for line in f:#for循环文件
if line.strip():
line_list = line.strip().split(',')
if int(line_list[0]) >= int(user_id):#文件内的id与user_id进行比较,如果文件内的id大,执行下面语句
user_id = int(line_list[0])#赋值给我们定义的user_id
user_id += 1#user_id 自加1
with open('mysql', 'a', encoding='utf-8') as f1:#打开新文件
f1.write('\n'+str(user_id)+','+user_str)#写入内容
print('添加成功') def update():
s=input('请输入要修改的数据,例如:set age=100 where name=chen\n')
if 'set' and 'where' in s:
flag=False
content, condition = s.split('where')#以where分割
content=''.join(content.split('set '))#以set+空格分割
if content.strip() and condition.strip():#判断是否都不为空,不为空就执行下列语句
update_name, update_value = content.strip().split('=')#以=分割,赋值给update_name,updata_value,这是要更新的值
name, value = condition.strip().split('=')#以=分割,赋值给name,vlue,这是条件
with open('mysql', encoding='utf-8') as f:#打开文件
for line in f:
if line.strip():
line_list = line.strip().split(',')#以逗号的形式分割成列表
if line_list[name_list.index(name.strip())] == value:#如果列表里面的值等于条件的值,执行下面语句
flag = True
if flag:#如果flag 为True的话,执行下列语句
with open('mysql', encoding='utf-8') as f, open('mysql.bak', 'w', encoding='utf-8') as f1:#打开文件
for line in f:
if line.strip():
line_list = line.strip().split(',')
if line_list[name_list.index(name.strip())] == value:#如果列表中的值等于条件的值
line_list[name_list.index(update_name.strip())] = update_value#那么就直接将对应索引位置的值修改为更新的值
f1.write(','.join(line_list) + '\n')#写入文件
print('更新成功')
else:#如果不等于条件的值
f1.write(line)#直接写入
if flag:#如果flag为True,才要删除和重命名,为False,则不用
os.remove('mysql')
os.rename('mysql.bak', 'mysql')
if not flag:
print('无法找到相对应的数据')
else:
print('语句格式错误')
else:
print('输入错误') @auth
def main():
msg={
'':check,
'':delete,
'':add,
'':update
}
print('''
1:查询
2:删除
3:添加
4:更新
''')
num=input('请选择要执行的操作\n')
msg[num]()
while __name__=='__main__':
main()

python练习题-员工信息表的更多相关文章

  1. python作业员工信息表程序(第四周)

    作业需求: 1. 员工信息表程序,实现增删改查操作: 2. 可进行模糊查询,语法至少支持下面3种: select name,age from staff_table where age > 22 ...

  2. day12 python作业:员工信息表

    作业要求: 周末大作业:实现员工信息表文件存储格式如下:id,name,age,phone,job1,Alex,22,13651054608,IT2,Egon,23,13304320533,Tearc ...

  3. python写员工信息表作业笔记

    需求 流程图

  4. python-查询员工信息表

    python查询员工信息表 基本要求: 用户可以模糊查询员工信息 显示匹配了多少条,匹配字符需要高亮显示 代码: #!/usr/env python #coding:utf-8 import time ...

  5. python基础之员工信息表作业

    周末大礼包 文件存储格式如下: id, name, age, phone, job 1, Alex, 22, 13651054608, IT 2, Egon, 23, 13304320533, Tea ...

  6. python's sixteenth day for me 员工信息表

    import os user_dic = { 'username':None, 'password':None, 'login':True } flag = False name_list = ['i ...

  7. python-打印简单公司员工信息表

    python-打印简单公司员工信息表 要求,输入name不为空,输入次数最多3次,3次后跳出程序: 知识点: raw_input str转int whil if elif else continue ...

  8. python3 员工信息表

    这是最后一条NLP了......来吧 十二,动机和情绪总不会错,只是行为没有效果而已 动机在潜意识里,总是正面的.潜意识从来不会伤害自己,只会误会的以为某行为可以满足该动机,而又不知道有其他做法的可能 ...

  9. 无废话ExtJs 入门教程十五[员工信息表Demo:AddUser]

    无废话ExtJs 入门教程十五[员工信息表Demo:AddUser] extjs技术交流,欢迎加群(201926085) 前面我们共介绍过10种表单组件,这些组件是我们在开发过程中最经常用到的,所以一 ...

随机推荐

  1. [转]调试利器-SSH隧道

    在开发微信公众号或小程序的时候,由于微信平台规则的限制,部分接口需要通过线上域名才能正常访问.但我们一般都会在本地开发,因为这能快速的看到源码修改后的运行结果.但当涉及到需要调用微信接口时,由于不和你 ...

  2. Atitit 关于处理环保行动联盟和动物解放阵线游击队的任命书 委任状

    Atitit 关于处理环保行动联盟和动物解放阵线游击队的任命书 委任状 Uke 集团文化部部长兼emir 大酋长圣旨到!! In god we trust ,Emir Decree大酋长圣旨:: En ...

  3. django项目settings.py的基础配置

    一个新的django项目初始需要配置settings.py文件: 1. 项目路径配置 新建一个apps文件夹,把所有的项目都放在apps文件夹下,比如apps下有一个message项目,如果不进行此项 ...

  4. MXNET:卷积神经网络

    介绍过去几年中数个在 ImageNet 竞赛(一个著名的计算机视觉竞赛)取得优异成绩的深度卷积神经网络. LeNet LeNet 证明了通过梯度下降训练卷积神经网络可以达到手写数字识别的最先进的结果. ...

  5. 100BASE-TX、100Base-FX等含义

    100BASE-TX:双绞线,使用两对非屏蔽双绞线或两对1类屏蔽双绞线连接,传输距离100米 100Base-FX,是在光纤上实现的100 Mbps以太网标准,其中F指示光纤,IEEE标准为802.3 ...

  6. 【转载】多模式串匹配之AC自动机

    原文地址:https://www.cnblogs.com/codeape/p/3845375.html 目录 [隐藏] 一.概述 二.AC算法思想 三.字典树tire的构造 四.搜索路径的确定 附录: ...

  7. java使用代理请求https

    我本来在我本机写的代码,本机电脑是可以连外网没限制,对于https和http都可以.但是放在linux服务器上后,因为VM限制了不能访问外网,而且有ssl验证所以就一直报错,要么是连不上线上请求,要么 ...

  8. SpringBoot------整合MyBatis

    1.添加pom.xml需要的依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="htt ...

  9. 写在开始前---ajax中的会话过期与重新登录

    一般情况下,点击<a>链接或浏览器输入url时,请求到后端,服务器判断会话是否过期.过期,重定向到登录页,或返回登录页的页面.在ajax中,返回重定向无效,这个时候就需要自己在ajax的逻 ...

  10. Ubuntu16.04首次root登录设置

    一.首次登录root模式设置 当第一次安装并登录Ubuntu16.04系统时,系统默认只能使用guest模式登录.登录系统后,在图像界面的右上方的系统设置中可转换为普通用户模式. 在普通登录模式下,经 ...