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()