2017-6-16 周末作業
阿新 • • 發佈:2017-06-18
spl 賦值 ble lin input 模塊 taf itl true
import os #調用os,time模塊 import time def file_oper(): #定義file_oper()對文件進行操作 with open(‘staff_table.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函數
2017-6-16 周末作業