lex utf 返回 false 数据 int 位置 files
文件存储格式如下:
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 #看起来像 ‘abc’in ‘1232abcahj’
#充分的利用函数
#文件处理 字符串处理
#str #where split
进阶选做:
b.可创建新员工记录,id要顺序增加
c.可删除指定员工记录,直接输入员工id即可
d.修改员工信息
语法:set 列名=“新的值” where 条件
#先用where查找对应人的信息,再使用set来修改列名对应的值为“新的值”
注意:要想操作员工信息表,必须先登录,登陆认证需要用装饰器完成
其他需求尽量用函数实现
- sos=True
- def krapper(flot):
- def inner(*args,**kwargs):
- global sos
- while sos:
- x=input(‘说账号:‘)
- y=input(‘说密码:‘)
- with open(‘user‘,encoding=‘utf-8‘) as files:
- for line in files:
- val=line.strip().split(‘|‘)
- if val[0]==x and int(val[1])==int(y):
- print(‘登录成功‘)
- sos=False
- break
- else:
- print(‘错啦,再来‘)
- ret=flot(*args,**kwargs)
- return ret
- return inner
- # 此程序为初始的登录功能项,能链接到各个分功能
- @krapper
- def MySQL():
- print(‘请选择执行的《功能项》||查询||、||修改||、||创建||、||删除||‘)
- choice = input(‘>>>‘)
- if choice == ‘查询‘:
- select()
- elif choice == ‘修改‘:
- revise()
- elif choice == ‘创建‘:
- appd()
- elif choice == ‘删除‘:
- cut()
- # 此程序为具有查询功能的函数
- @krapper
- def select():
- print("""‘select name,age where age>22 例如:> < ‘
- ‘select * where job=IT
- ‘select * where phone like 133 例如:‘abc’in ‘1232abcahj’‘
- ‘退出操作请直接输入||q||,返回《功能项》请直接输||回程||
- """)
- stop = True
- while stop:
- search = input(‘请按照上述格式所述方法输入内容:‘)
- if search==‘q‘:
- stop=False
- elif search==‘回程‘:
- MySQL()
- else:
- dic={‘id‘:0,‘name‘:1,‘age‘:2,‘phone‘:3,‘job‘:4}
- show_list=[]
- handle=search.strip().split(‘where‘) #[‘...name,age‘,‘age>22‘]
- with open(‘员工信息表‘,‘r‘,encoding=‘utf-8‘) as files_1:
- for line in files_1:
- line_x=line.strip().split(‘,‘) #[1,name,age,phone,job]
- if ‘>‘ in handle[1]:
- handle_1=handle[1].strip().split(‘>‘) #[‘age‘,‘22]
- handle_2=handle[0].replace(‘select‘,‘‘).strip().split(‘,‘) #[‘name‘,‘age‘]
- if int(line_x[dic[handle_1[0]]]) > int(handle_1[1]) :
- show_list.append((line_x[dic[handle_2[0]]],line_x[dic[handle_2[1]]]),)
- elif ‘<‘ in handle[1]:
- handle_1=handle[1].strip().split(‘<‘) #[‘age‘,‘25]
- handle_2=handle[0].replace(‘select‘,‘‘).strip().split(‘,‘) #[‘name‘,‘age‘]
- if int(line_x[dic[handle_1[0]]]) < int(handle_1[1]) :
- show_list.append((line_x[dic[handle_2[0]]],line_x[dic[handle_2[1]]]),)
- elif ‘=‘ in handle[1]:
- handle_1=handle[1].strip().split(‘=‘) #[‘job‘,‘IT‘]
- if line_x[dic[handle_1[0]]] == handle_1[1] :
- show_list.append(line_x)
- elif ‘like‘in handle[1]:
- handle_1 = handle[1].strip().split(‘like‘) # [‘phone‘,‘133‘]
- if handle_1[1].strip() in line_x[dic[handle_1[0].strip()]]:
- show_list.append(line_x)
- else:
- print(‘查询成功,龙龙好帅‘)
- print(show_list)
- return
- # 此程序为具有修改功能的函数
- @krapper
- def revise():
- dic = {‘id‘: 0, ‘name‘: 1, ‘age‘: 2, ‘phone‘: 3, ‘job‘: 4}
- print(""">>>请选择示例中的一个元素来识别要修改员工的信息:
- >>>示例:id=xx,name=xx,age=xx,phone=xx,job=xx;xx为要输入内容
- >>>输入||q||,退出操作;输入||回程||返回《功能项》
- >>>正确输入格式为:name=xx,想替换的内容phone=yy,替换后的内容zz
- >>>既:name=xx,phone=yy,zz
- """)
- stop = True
- while stop:
- print(‘输入q,退出操作‘)
- x = input(‘输入想替换的内容:‘)
- if x == ‘q‘:
- stop = False
- elif x==‘回程‘:
- MySQL()
- else:
- lis_cut = x.strip().split(‘,‘) # [name=xx,phone=yy,zz]
- lis_cut_1 = lis_cut[0].split(‘=‘) # [name,xx]
- lis_cut_2 = lis_cut[1].split(‘=‘) # [phone,yy]
- with open(‘员工信息表‘, ‘r‘, encoding=‘utf-8‘) as files_r:
- with open(‘员工信息表_bak‘, ‘w‘, encoding=‘utf-8‘) as files_w:
- for line in files_r:
- line_lis = line.strip().split(‘,‘)
- if line_lis[1] == lis_cut_1[1]:
- line_r = line.replace(str(line_lis[dic[lis_cut_2[0]]]), str(lis_cut[2]))
- files_w.write(line_r)
- else:
- files_w.write(line)
- import os
- os.remove(‘员工信息表‘)
- os.rename(‘员工信息表_bak‘, ‘员工信息表‘)
- print(‘修改成功,龙龙巨帅‘)
- return
- # 此程序为具有添加功能的函数
- @krapper
- def appd():
- stop = True
- while stop:
- print(""">>>按照此顺序和方法输入增加信息
- >>>示例:name,age,phone,job
- >>>示例:xxxx,yyy,zzzzz,ooo
- """)
- inner = input(‘>>>‘)
- if inner == ‘q‘ :
- stop = False
- elif inner==‘回程‘:
- MySQL()
- else:
- with open(‘员工信息表‘, ‘r‘, encoding=‘utf-8‘) as files_r:
- with open(‘员工信息表‘, ‘a‘, encoding=‘utf-8‘) as files_a:
- start=0
- for line in files_r:
- start+=1
- else:
- line_add=‘\n‘+ str(start+1)+str(‘,‘)+str(inner)
- files_a.write(line_add)
- print(‘添加成功,龙龙超级帅‘)
- return
- # 此程序为具有删除功能的函数,可实现序号更新
- @krapper
- def cut():
- print(""">>>请选择示例中的两个元素来识别要删除的员工信息:
- >>>示例:name=xx,age=xx,phone=xx,job=xx
- >>>其中,xx为要输入内容
- """)
- dic = {‘name‘: 0, ‘age‘: 1, ‘phone‘: 2, ‘job‘: 3}
- stop = True
- while stop:
- cutting = input(‘>>>请输入您想删除的内容,输入q则退出操作‘)
- if cutting == ‘q‘:
- stop = False
- elif cutting==‘回程‘:
- MySQL()
- else:
- lis_cut = cutting.strip().split(‘,‘) # [name=xx,age=xx]
- lis_cut_1 = lis_cut[0].split(‘=‘) # [name,xx]
- lis_cut_2 = lis_cut[1].split(‘=‘) # [age,xx]
- with open(‘员工信息表‘, ‘r‘, encoding=‘utf-8‘) as files_r:
- with open(‘员工信息表_bak‘, ‘w‘, encoding=‘utf-8‘) as files_w:
- start = 0
- for line in files_r:
- line_r = line.strip().split(‘,‘)
- line_r.remove(line_r[0])
- if line_r[dic[lis_cut_1[0]]] == lis_cut_1[1] and line_r[dic[lis_cut_2[0]]] == lis_cut_2[1]:
- continue
- else:
- start += 1
- line_all = str(start) + ‘,‘ + str(line_r[0]) + ‘,‘ + str(line_r[1]) + ‘,‘ + str(
- line_r[2]) + ‘,‘ + str(line_r[3]) + ‘\n‘
- files_w.write(line_all)
- import os
- os.remove(‘员工信息表‘)
- os.rename(‘员工信息表_bak‘, ‘员工信息表‘)
- print(‘删除成功,龙龙无敌帅‘)
- return
- # 此处的mysql()后期可指向固定位置,对文件做各种操作,具有简单数据库功能
- MySQL()
Python开发【第xxx篇】函数练习题-----员工信息表
来源: http://www.bubuko.com/infodetail-2290622.html