You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
12 KiB

# -*- coding: utf-8 -*-
"""
Created on Mon Dec 5 08:36:06 2022
@author: shipeilong
"""
import pandas as pd
from datetime import datetime
# pandas显示设置
# #显示所有列
# pd.set_option('display.max_columns', None)
# #显示所有行
# pd.set_option('display.max_rows', None)
# #设置value的显示长度为100默认为50
# pd.set_option('max_colwidth',100)
# 格式输出
def print_message(*message):
print("{}】 【{}".format(datetime.now(), message))
# 读取stu.csv文件
def read_stu_csv():
stus = pd.read_csv("stu.csv", index_col= False)
return stus
# 查询全部学生信息
def read_all_stus():
stus = read_stu_csv()
display(stus)
# 通过姓名查询某个学生信息
def read_someone_by_name():
stus = read_stu_csv()
stu_names = stus['姓名'].values.tolist()
display(stus)
while True:
print_message("请输入查询学生姓名:")
stu_name = input("姓名:")
if stu_name not in stu_names:
print_message("学生不存在,请重新输入!")
continue
else:
stu = stus.loc[(stus['姓名'] == stu_name)]
print_message("{}】学生信息查询成功!".format(stu_name))
display(stu)
break
# 通过电话查询某个学生信息
def read_someone_by_phone():
stus = read_stu_csv()
display(stus)
stu_phones = stus['电话'].values.tolist()
while True:
print_message("请输入查询学生电话:")
stu_phone = input("电话:")
if int(stu_phone) not in stu_phones:
print_message("学生不存在,请重新输入!")
continue
else:
stu = stus.loc[(stus['电话'] == int(stu_phone)),:]
print_message("{}】学生信息查询成功!".format(stu_phone))
display(stu)
break
# 通过姓名/电话查询某个学生信息
def read_stu():
stus = read_stu_csv()
stu_names = stus['姓名'].values.tolist()
stu_phones = stus['电话'].values.tolist()
while True:
stu_input = input("请输入学生姓名/电话:")
if stu_input in stu_names:
stu = stus.loc[(stus['姓名'] == stu_input),:]
display(stu)
break
if int(stu_input) in stu_phones:
stu = stus.loc[(stus['电话'] == int(stu_input)),:]
display(stu)
break
else:
print_message("未查询到【{}】信息,请重新输入".format(stu_input))
continue
# 增加某个学生信息
def add_stu():
stus = read_stu_csv()
stu_names = stus['姓名'].values.tolist()
stu_phones = stus['电话'].values.tolist()
while True:
# 添加学生姓名
stu_name_flag = True
while stu_name_flag:
stu_name = input("请输入要添加的学生姓名:")
if stu_name == "":
print_message("学生姓名输入不能为空,请重新输入!")
continue
else:
break
# 添加学生电话
stu_phone_flag = True
while stu_phone_flag:
stu_phone = input("请输入该学生电话:")
if stu_phone == "":
print_message("学生电话输入不能为空,请重新输入!")
continue
else:
stu_phone_flag = False
# 添加学生生日
stu_birth_flag = True
while stu_birth_flag:
stu_birth = input("请输入该学生生日格式2000/1/1")
if stu_birth == "":
print_message("学生生日输入不能为空,请重新输入!")
continue
else:
stu_birth_flag = False
# 添加学生入学年份
year_flag = True
while year_flag:
year = input("请输入该学生入学年份默认为2020")
if year == "":
print_message("输入为空默认学生入学年份为2020")
year = 2020
year_flag = False
else:
year_flag = False
last_id = stus.iloc[-1].to_dict()["学号ID"]
stu_dict = {"学号ID":str(int(last_id)+1),"姓名":stu_name,"电话":stu_phone,"生日":stu_birth,"入学年份":year}
stu_df = pd.DataFrame(stu_dict,index=[0])
new_stus = stus.append(stu_df, sort=False)
new_stus.to_csv("stu.csv", index = False)
print_message("添加学生【{}】成功".format(stu_name))
display(stu_df)
break
#通过姓名修改某个学生姓名
def update_by_name():
stus = read_stu_csv()
display(stus)
stu_names = stus['姓名'].values.tolist()
while True:
stu_name = input("请输入待修改学生姓名")
if stu_name not in stu_names:
print_message("学生不存在,请重新输入!")
continue
else:
stu = stus.loc[(stus['姓名'] == stu_name),:]
display(stu)
name_flag = True
while name_flag:
name = input("请输入修改姓名:")
if name == "":
print_message("修改姓名不能为空,请重新输入!")
continue
else:
name_flag = False
add_flag = True
while add_flag:
confirm = input("{}】修改为【{}】元请确认y/n".format(stu_name, name))
if confirm == "y":
# 获取”姓名“index
stu_index = list(stus.columns).index('姓名')
# 获取学生index
name_index = stus.loc[(stus['姓名'] == stu_name),:].index[0]
# 获取学生原姓名
stu_name_dict = stu.to_dict()["姓名"]
stu_name_index = list(stu_name_dict.keys())[0]
stu_name = stu_name_dict[stu_name_index]
# 增加学生添加姓名
stus.iloc[name_index,stu_index] = name
# 写入stu表中(stu.csv)
stus.to_csv("stu.csv", index = False)
print_message("{}】添加【{}】成功".format(stu_name, name))
stu = stus.loc[(stus['姓名'] == name),:]
display(stu)
print_message("最新学生联系管理系统学生名单如下:")
display(stus)
break
elif confirm == "n":
print_message("取消添加!")
break
else:
print_message("输入错误,请重新输入!")
continue
break
# 通过电话修改某个学生电话
def update_by_phone():
stus = read_stu_csv()
display(stus)
stu_phones = stus['电话'].values.tolist()
while True:
stu_phone = input("请输入待修改学生电话")
if int(stu_phone) not in stu_phones:
print_message("学生不存在,请重新输入!")
continue
else:
stu = stus.loc[(stus['电话'] == int(stu_phone)),:]
display(stu)
phone_flag = True
while phone_flag:
phone = input("请输入修改电话:")
if phone == "":
print_message("修改电话不能为空,请重新输入!")
continue
else:
phone_flag = False
add_flag = True
while add_flag:
confirm = input("{}】修改为【{}】元请确认y/n".format(stu_phone, phone))
if confirm == "y":
# 获取”电话“index
stu_index = list(stus.columns).index('电话')
# 获取学生index
phone_index = stus.loc[(stus['电话'] == int(stu_phone)),:].index[0]
# 获取学生原电话
stu_phone_dict = stu.to_dict()["电话"]
stu_phone_index = list(stu_phone_dict.keys())[0]
stu_phone = stu_phone_dict[stu_phone_index]
# 增加学生添加电话
stus.iloc[phone_index,stu_index] = phone
# 写入stu表中(stu.csv)
stus.to_csv("stu.csv", index = False)
print_message("{}】添加【{}】成功".format(stu_phone, phone))
stu = stus.loc[(stus['电话'] == phone),:]
display(stu)
print_message("最新学生联系管理系统学生名单如下:")
display(stus)
break
elif confirm == "n":
print_message("取消添加!")
break
else:
print_message("输入错误,请重新输入!")
continue
break
# 更改学生信息
def update_stu():
stus = read_stu_csv()
stu_names = stus['姓名'].values.tolist()
stu_phones = stus['电话'].values.tolist()
display(stus)
input_flag = True
while input_flag:
stu_input = input("请输入学生姓名/电话:")
if stu_input in stu_names:
stu = stus.loc[(stus['姓名'] == stu_input),:]
input_flag = False
break
if int(stu_input) in stu_phones:
stu = stus.loc[(stus['电话'] == int(stu_input)),:]
input_flag = False
break
else:
print_message("未查询到【{}】信息,请重新输入".format(stu_input))
continue
display(stu)
while True:
thing = input("请输入要修改的类型('学号ID', '姓名', '电话', '生日', '入学年份'")
if thing not in ["学号ID", "姓名", "电话", "生日", "入学年份"]:
print_message("类型选择有误,请重新输入")
continue
else:
# 获取学生原信息
stu_dict = stu.to_dict()[thing]
stu_index = list(stu_dict.keys())[0]
stu_one = stu_dict[stu_index]
print_message("原信息为: {}". format(stu_one))
new_one = input("请输入新内容:")
print_message("您确定将【{}】修改为【{}】吗?".format(stu_one, new_one))
confirm_flag = input("y/n")
if confirm_flag == "y":
thing_index = list(stus.columns).index(thing)
stus.iloc[stu_index, thing_index] = new_one
# 写入stu表中(stu.csv)
stus.to_csv("stu.csv", index = False)
display(stus)
break
# 删除学生信息
def del_stu():
stus = read_stu_csv()
stu_names = stus['学号ID'].values.tolist()
stu_phones = stus['电话'].values.tolist()
while True:
stu_input = eval(input("请输入学生学号:"))
if stu_input in stu_names:
print_message("您确定删除此学生吗?")
confirm_flag = input("y/n")
if confirm_flag == 'y':
x=stus[stus['学号ID']==stu_input].index.tolist()[0]
stus.drop(x,axis=0,inplace=True)
print('删除成功')
stus.to_csv("stu.csv", index = False)
else :
print('终止操作')
display(stus)
break
else:
print('重新输入姓名')
print('你想查询的内容')
print('0.查询全部学生信息')
print('1.通过姓名查找学生信息')
print('2.通过电话查找学生信息')
print('3.通过姓名/电话查找学生信息')
print('4.通过姓名修改学生信息')
print('4.通过电话修改学生信息')
print('6.添加学生信息')
print('7.更新学生信息')
print('8.删除学生信息')
x=eval(input('请输入:'))
if x==1:
read_someone_by_name()
elif x==2:
read_someone_by_phone()
elif x==3:
read_stu()
elif x==4:
update_by_name()
elif x==5:
update_by_phone()
elif x==6:
add_stu()
elif x==7:
update_stu()
elif x==0:
read_all_stus()
elif x==8:
del_stu()
else:
print('输入错误,重新输入')