import os #调用os,time模块import time def file_oper(): #定义file_oper()对文件进行操作 with open(‘message.txt‘,‘r‘,encoding=‘utf-8‘) as f: #打开文件,生成两个列表 file_opers = [] file_oper1 = [] for line in f: file_opers.append(line.strip().split(‘,‘)) file_oper1.append(line) file_opera = [file_opers,file_oper1] #将两个列表添加到list中 return file_opera #返回list的值 def list(): #定义list函数 file_opers = file_oper()[0] #调用file_oper函数,将file_opers赋值 index = [] #定义空列表 staffids = [] names = [] ages = [] phones = [] depts = [] enroll_datas = [] for (v,i) in enumerate(file_opers): #获取下标 index.append(v) #将下标添加到index列表中 for v in index: #将每个列表中下标相同的元素放到一个列表中 staffids.append(file_opers[v][0]) names.append(file_opers[v][1]) ages.append(file_opers[v][2]) phones.append(file_opers[v][3]) depts.append(file_opers[v][4]) enroll_datas.append(file_opers[v][5]) value = [staffids,names,ages,phones,depts,enroll_datas] #将所有列表添加到新的列表value return value #返回列表value的值 def title(): #定义title函数 print(‘您当前可操作的对象为:‘) #打印可操作对象 print(‘------------------------------------‘) for x in file_oper()[1]: print(x.strip()) print(‘------------------------------------‘) def example(): #定义exmaple函数,打印语句示例 oper = [‘增:create zhang san,23,1388888888,Market,2018-03-31‘,\ ‘删:delete 1‘,\ ‘改:update staff_table SET dept="Market" WHERE where dept = "IT"‘,\ ‘查:select name,age from staff_table where age > 22,\ \nselect * from staff_table where dept = "IT",\ \nselect * from staff_table where enroll_date like "2013"‘] print(‘--------操作语句示例---------‘) for i in oper: print(i) print(‘-----------------------------‘) def rename(func): #定义rename装饰器,对操作的文件进行备份 def wapper(*args,**kwargs): func(*args,**kwargs) os.rename(‘message.txt‘, ‘message_%s.bak‘ % time.strftime(‘%Y%m%d%H%M%S‘)) os.rename(‘message_1.txt‘, ‘message.txt‘) return wapper @rename #调用装饰器def create(str1): #定义create函数 phones = list()[3] #调用函数并赋值 staff_id = list()[0] list1 = file_oper()[1] createdata = str1.split(‘ ‘) #对输入语句进行split操作,取得需要的phone值 staff = createdata[-1] staff_a = staff.split(‘,‘) with open(‘message_1.txt‘, ‘w+‘, encoding=‘utf-8‘) as add_file: #打开文件 if staff_a[2] not in phones: #判断输入是否在phones列表中 index1 = int(staff_id[-1])+1 #存在则将staff_id+1,存入message_1.txt中 staff_all = ‘\n‘ + str(index1) + ‘,‘ + staff list1.append(staff_all) print(‘-----------------------‘) print(‘写入成功!‘) else: #不存在文件重新写入 print(‘信息已存在或输入错误,请重新输入‘) for i in list1: add_file.write(i) @rename #调用装饰器def delete(str1): #定义delete函数 staffids = list()[0] #调用函数并赋值 list1 = file_oper()[1] deldata = str1.split(‘ ‘) #对输入语句进行split操作,取得需要的staff_id值 staff_id = deldata[-1] with open(‘message_1.txt‘, ‘w+‘, encoding=‘utf-8‘) as del_file: #打开文件 if staff_id in staffids: #判断文件是否存在于staffids列表中 index = staffids.index(staff_id) #存在删除 del staffids[int(index)] del list1[int(index)] print(‘-----------------------‘) print(‘删除成功!‘) else: #不存在 print(‘文件中不存在对应的staffid!‘) for i in list1: del_file.write(i) @rename #调用装饰器def update(str1): #定义update函数 depts = list()[4] #调用函数并赋值 file_opers = file_oper()[0] list1 = file_oper()[1] update_data = str1 #对输入语句进行split操作,取得dept值 update_datas = update_data.split(‘"‘) with open(‘message_1.txt‘, ‘w+‘, encoding=‘utf-8‘) as up_file: #打开文件 if update_datas[3] in depts: #判断dept是否depts列表中 for i in file_opers: #存在修改 if update_datas[3] in i: i[i.index(update_datas[3])] = update_datas[1] i = ",".join(i) up_file.write(str(i) + "\n") print(‘------------------------------‘) print(‘更改成功!‘) else: #不存在打印提示 for i in list1: up_file.write(i) print(‘所输入的值不在文件中!‘) def select(str1): #定义select函数 list1 = file_oper()[0] #调用函数并赋值 choise = str1 #将输入的字符串进行split操作,取得需要的值 choise1 = choise.split(‘where‘) value = choise1[-1].strip() if value.startswith(‘age‘): #输入的值为age则查找文件中对应的age信息 choise_age = value.split(">") age = choise_age[-1].strip() print(‘---------------------‘) for i in list1: if int(age) < int(i[2]) : i = ",".join(i) print(i) print(‘---------------------‘) print(‘以上为查找到的信息!‘) if value.startswith(‘dept‘): #输入的值为dept则查找文件中对应的dept信息 choise_dept = value.split(‘"‘) print(‘---------------------‘) for i in list1: if choise_dept[1] in i: i = ",".join(i) print(i) print(‘---------------------‘) print(‘以上为查找到的信息!‘) if value.startswith(‘enroll_date‘): #输入的值enroll_date为则查找文件中对应的信息 choise_enroll = value.split(‘"‘) print(‘---------------------‘) for i in list1: if i[-1].startswith(choise_enroll[1]) : i = ",".join(i) print(i) print(‘---------------------‘) print(‘以上为查找到的信息!‘) def mains(): #定义mains函数 example() #打印示例 while True: title() #打印可操作语句 choise = input(‘请输入操作语句(按q退出):‘) #请用户输入语句 if choise.startswith(‘select‘): #根据开头输入单词,调用对应的函数,输入q为退出 select(choise) if choise.startswith(‘update‘): update(choise) if choise.startswith(‘delete‘): delete(choise) if choise.startswith(‘create‘): create(choise) if choise == ‘q‘: print(‘退出成功!‘) break mains() #调用mains函数
时间: 2024-11-05 21:57:08