員工資訊增刪改查程式
阿新 • • 發佈:2018-12-21
import pandas as pd import codecs import csv def load_staff_date(): dates=[ [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'] ] fileName = 'StaffDate.csv' filenames = ['staff_id',"name","age","phone_number","dept","enroll_date"] data = pd.DataFrame(columns=filenames) data.to_csv('StaffDate.csv', mode='w+', index=False, encoding='utf-8') with codecs.open(fileName, ' a+ ' , ' utf-8') as csvfile: csv_write = csv.writer(csvfile) for dateline in dates: csv_write.writerow(dateline) # df = pd.read_csv(fileName) # df.head(10) def find_data(cmd): factor, term, require, condition = cmd.split()[1], cmd.split()[5], cmd.split()[6], cmd.split()[7] df = pd.read_csv("StaffDate.csv") if condition.isdigit(): condition = int(condition) else: condition = condition.upper()[1:-1] # 設定flag標記語句第一個字串是否為* flag = 1 if factor == '*': flag = 0 if flag == 1: factor = factor.split(',') if require == '=': finds = df[df[term] == condition] if flag == 1: for word in factor: print(word) print(finds[word]) else: print(finds) elif require == 'like': test = [] Test = [] with open("StaffDate.csv", 'r', encoding='utf-8')as f: read = csv.reader(f) for i, line in enumerate(read): if len(line) == 6: test.append(line) i += 1 # print(test) for i, line in enumerate(test): if line[5][0:4] == str(condition): Test.append(line) for line in Test: print(line) elif require == '>': print(2) finds = df[df[term] > condition] if flag == 1: for word in factor: print(finds[word]) else: print(finds) else: print(3) finds = df[df[term] < condition] if flag == 1: for word in factor: print(finds[word]) else: print(finds) def del_staff_data(number): df = pd.read_csv('StaffDate.csv') df = df[~df['staff_id'].isin([number])] count = len(df['staff_id']) print(df.head(count)) #df.to_csv('StaffDate.csv', mode='w+', index=False, encoding='utf-8') def add_staff_data(cmd): ''' 對add staff_table Alex Li,25,134435344,IT,2015‐10‐29進行處理只擷取 後面的Alex Li,25,134435344,IT,2015‐10‐29賦值為臨時變數b,並整合Alex 和Li 還有 對電話加上尖括號,因為csv檔案處理長整形時會自動轉為科學計演算法。 ''' cmd_mid = cmd.split()[2] cmd_last = list(cmd.split()[3].split(',')) cmd_last[0] = cmd_mid + " " + cmd_last[0] cmd_last[2] = "<" + cmd_last[2] + '>' name, age, phone, dept, enroll_date = cmd_last[0], cmd_last[1], cmd_last[2], cmd_last[3], cmd_last[4] df = pd.read_csv('StaffDate.csv') for ph in df['phone_number']: if phone == ph: print("該電話已存在") exit() count = len(df['staff_id']) + 1 df.loc[count, :] = [count, name, age, name, dept, enroll_date] print(df.head(count)) #df.to_csv('StaffDate.csv', mode='w+', index=False, encoding='utf-8') def update_staff_data(contact, current_group, current_value): Test = contact.split('=') target_group = Test[0] target_value = Test[1] # 去除‘’ if target_value.isdigit(): pass else: target_value = target_value[1:-1] current_value = current_value[1:-1] df = pd.read_csv('StaffDate.csv') # 這裡預設只有dept這一列是全改其他都是改一個 if target_group == 'dept': target_value = target_value.title() current_value = current_value.upper() df['dept'] = df['dept'].replace([current_value], [target_value]) elif target_group == 'age': current_value = current_value.title() replace_value = int(df[df[current_group].isin([current_value])][target_group]) df[df[current_group].isin([current_value])] = df[df[current_group].isin([current_value])].replace( [replace_value], [target_value]) count = len(df['staff_id']) print(df.head(count)) #df.to_csv('StaffDate.csv', mode='w+', index=False, encoding='utf-8') def syntax_parser(cmd): if 'add' == cmd.split()[0]: if cmd.split()[1] == "staff_table": add_staff_data(cmd) elif 'find' == cmd.split()[0]: if cmd.split()[2] == 'from' and cmd.split()[3] == 'staff_table' and cmd.split()[4] == 'where': find_data(cmd) elif 'del' == cmd.split()[0]: if "del from staff where id=" == cmd[0:24]: del_staff_data(int(cmd[24:])) elif 'update' == cmd.split()[0]: if cmd.split()[1] == 'staff_table' and cmd.split()[2] == "set" and cmd.split()[4] == 'where': if cmd.split()[5] == 'name': Name = cmd.split()[7] + " " + cmd.split()[8] else: Name = cmd.split()[7] update_staff_data(cmd.split()[3], cmd.split()[5], Name) else: print("語句不合法") def main(): load_staff_date() cmd = input("請輸入解析語句:").strip(" ").lower() syntax_parser(cmd) if __name__ == '__main__': main()