#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

import re
import os


# 1.可进行模糊查询,语法至少支持下面3种查询语法:
# find name,age from staff_table where age > 22 #找出年龄大于22岁的所有人,并返回 name和age信息
# find * from staff_table where dept = "IT" #找出所有部门式IT的人的信息
# find * from staff_table where enroll_date like "2013" # 模糊查询找出所有2013年入职的人
# 2 添加员⼯工信息(⽤用户的⼿手机号不不允许重复) 添加员⼯工信息(⽤用户的⼿手机号不不允许重复)
# add staff_table Mosson,18,13678789527,IT,2018-12-11
# 3 删除员⼯工信息(根据序号删除相应员⼯工的信息) 删除员⼯工信息(根据序号删除相应员⼯工的信息)
# del from staff_table where id = 10
# 需求:从staff_table中删除序号id为10的这⼀一条员⼯工的信息
# 4.可修改员工信息,语法如下:
# update staff_table SET dept="Market" WHERE dept = "IT" 把所有dept=IT的纪录的dept改成Market
# update staff_table SET age=25 WHERE name = "Alex Li" 把name=Alex Li的纪录的年龄改成25
# 5.以上每条语名执行完毕后,要显示这条语句影响了多少条纪录。 比如查询语句 就显示 查询出了多少条、修改语句就显示修改了多少条等。


def make_dict_list(flie_name):
'''将文件内容转化一下,列表中套嵌字典的格式'''
with open(flie_name, 'r', encoding='utf-8') as read_f:
li = [{'id': i.split(',')[0], 'name': i.split(',')[1],
'age': i.split(',')[2], 'phone': i.split(',')[3],
'dept': i.split(',')[4], 'enroll_date': i.split(',')[5]} for i in read_f]
return li

# 查找
def find_staff_info(file_name, show_contents, match_rules):
list_dic = make_dict_list(flie_name=file_name)
new_staff_info = []
# if match_rules[0] == 'id' or match_rules[0] == 'age':
# if match_rules[1] == '>': # 进行分割之后如果数量大于1, 那么判断的就是age>22的
# new_staff_info = [i for i in list_dic if int(i[match_rules[0]]) > int(match_rules[2])]
# if match_rules[1] == '<':
# new_staff_info = [i for i in list_dic if int(i[match_rules[0]]) < int(match_rules[2])]
# if match_rules[1] == '=':
# new_staff_info = [i for i in list_dic if int(i[match_rules[0]]) == int(match_rules[2])]
# for i in new_staff_info:
# for j in show_contents:
# print(j, i[j], end='\t')
# print('')
if match_rules[0] == 'id' or match_rules[0] == 'age':
if match_rules[1] == '>':
for i in list_dic:
if int(i[match_rules[0]]) > int(match_rules[2]):
new_staff_info.append(i)
if match_rules[1] == '<':
for i in list_dic:
if int(i[match_rules[0]]) < int(match_rules[2]):
new_staff_info.append(i)
if match_rules[1] == '=':
for i in list_dic:
if int(i[match_rules[0]]) == int(match_rules[2]):
new_staff_info.append(i)
# if match_rules[0] == 'dept':
# for i in list_dic:
# if i[match_rules[0]] == match_rules[2]:
# new_staff_info.append(i)
# if match_rules[0] == 'enroll_date':
# for i in list_dic:
# if i[match_rules[0]].startswith(match_rules[2]):
# new_staff_info.append(i)
if match_rules[0] in list_dic[1]:
for i in list_dic:
if match_rules[2] in i[match_rules[0]]:
new_staff_info.append(i)
count = 0
for i in new_staff_info:
for j in show_contents:
print(j, i[j].strip(), end='\t')
print('')
count += 1
print('查找到了%d条数据' % count)
return new_staff_info

#增加
def add_staff_info(file_name, staff_info):
num = 0 # 文件最后一行的员工id 这时还时一个字符串的数字
with open(file_name, 'rb',) as read_f:
for line in read_f:
offs = -30 # 设定偏移量,倒向查找所以为 负数
while True:
read_f.seek(offs, 2) # 2 为文件末尾开始查找的意思,偏移量倒移到-30的位置
date = read_f.readlines() # 将光标当前位置之后的内容 读取到列表中,直到遇到下一个\n 换行符时,算作另一个列表元素
if len(date) > 1: # 判断date元素个数,超过1了。那么最后一个元素就是,文件的最后一行内容
num = date[-1].decode('utf-8').split(',')[0] # 获取文件最后一行内容的第一个数字是多少
break
else:
offs *= 2
with open(file_name, 'r', encoding='utf-8') as f, open(file_name, 'a', encoding='utf-8') as write_f:
count = 0
for line_info in f:
if staff_info.split(',')[2] in line_info: # 判断电话是否重复
print('电话不允许重复')
break # 重复了就退出循环,后续代码不执行。
else:
new_staff_info = '\n%d,%s' % (int(num) + 1, staff_info) # 将数字+1 再和 传进来的列表进行拼接成一个文件内容
write_f.write(new_staff_info) # 写入内容
count += 1
print('增加了%d个员工'%count)

# 删除
def del_staff_info(file_name, match_rules):
count = 1 # 员工ID每次加+1
count_ont = 0
flag = False
with open(file_name, 'r', encoding='utf-8') as read_f, open('new_file', 'w', encoding='utf-8') as write_f:
for line in read_f:
staff_info_list = line.split(',', maxsplit=1)
if int(staff_info_list[0]) == int(match_rules[2]):
count_ont += 1
continue
else:
if not flag:
write_f.write('%d,%s' % (count, staff_info_list[1].strip()))
flag = True
else:
write_f.write('\n%d,%s' % (count, staff_info_list[1].strip()))
count += 1 # 员工ID每次加+1
print('删除了%d个员工信息' % count_ont)
os.rename(file_name, 'new_file.bak')
os.rename('new_file', file_name)
os.remove('new_file.bak')

#更新
def update_staff_info(file_name, old_info, new_info):
list_dic = make_dict_list(flie_name=file_name)
count = 0
for dic in list_dic:
if dic[old_info[0]] == old_info[2] and dic[new_info[0]] != new_info[2]:
dic[new_info[0]] = new_info[2]
count += 1
print('更换了%d'%count)
with open(file_name, 'w', encoding='utf-8') as write_f:
for i in list_dic:
staff_info_list = []
for j in i.values():
staff_info_list.append(j)
write_f.write(','.join(staff_info_list))

'''
find name,age from function_homework where age > 22
find * from function_homework where dept = IT
find * from function_homework where enroll_date like 2013

add function_homework Mosson,18,13678789527,IT,2018-12-11

del from function_homework where id = 10

update function_homework set dept = Market where dept = IT
update function_homework set age = '25' where name = "Alex Li"
'''
# 判断需求
def judge_demand(command):
if command.startswith('find'):
file_name = re.findall('(?<=from).+(?=where)', command)[0].strip() # 不论什么操作都需要统一的文件名
ret = command
if '*' in command: # 把带 * 号的.全部替换掉.
ret = command.replace('*', 'id,name,age,phone,dept,enroll_date', 1)
show_contents = re.findall('(?<=find).+(?=from)', ret)[0].strip().split(',') # 需要显示哪些信息.不需要过多处理
match_rules = re.findall('(?<=where).+', ret)[0].split(maxsplit=2) # 判断信息
find_staff_info(file_name, show_contents, match_rules)
if command.startswith('add'):
add_info_list = command.split(' ') # 使用空格分割字符串
add_file = add_info_list[1]
add_info = add_info_list[2]
add_staff_info(add_file, add_info)
if command.startswith('del'):
file_name = re.findall('(?<=from).+(?=where)', command)[0].strip()
match_rules = re.findall('(?<=where).+', command)[0].split(maxsplit=2)
del_staff_info(file_name, match_rules)
if command.startswith('update'):
file_name = re.findall('(?<=update).+(?=set)', command)[0].strip()
new_info = re.findall('(?<=set).+(?=where)', command)[0].strip().split(maxsplit=2)
old_info = re.findall('(?<=where).+', command)[0].strip().split(maxsplit=2)
update_staff_info(file_name, old_info, new_info)


if __name__ == '__main__':
command_str = input('请输入命令,除显示需求外,其余每个字符之间加空格:').replace('"', '')
new_command_str = command_str.replace('\'', '') # 把输入进来的字符串中的 ' " 全部替换
judge_demand(new_command_str)



# 1,Alex Li,22,13651054608,IT,2013-04-01
# 2,Jack Wang,28,13451024608,HR,2015-01-07
# 3,Rain Wang,21,13451054608,IT,2017-04-01
# 4,Mack Qiao,44,15653354208,Sales,2016-02-01
# 5,Rachel Chen,23,13351024606,IT,2013-03-16
# 6,Eric Liu,19,18531054602,Marketing,2012-12-01
# 7,Chao Zhang,21,13235324334,Administration,2011-08-08
# 8,Kevin Chen,22,13151054603,Sales,2013-04-01
# 9,Shit Wen,20,13351024602,IT,2017-07-03
# 10,Shanshan Du,26,13698424612,Operation,2017-07-02
# 11,Mosson,18,13188888888,IT,2018-12-11
posted on 2019-01-07 11:16  rookiehbboy  阅读(446)  评论(0编辑  收藏  举报