From c9a3a687b2b7e188d1c72566c0e034169ab33126 Mon Sep 17 00:00:00 2001 From: hnu202109060103 Date: Fri, 9 Dec 2022 19:31:50 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=E5=AD=A6=E7=94=9F=E7=AE=A1=E7=90=86.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 学生管理.py | 313 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 313 insertions(+) create mode 100644 学生管理.py diff --git a/学生管理.py b/学生管理.py new file mode 100644 index 0000000..c8a49ad --- /dev/null +++ b/学生管理.py @@ -0,0 +1,313 @@ + +# -*- 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",encoding=('GB2312'),index_col= False) + + return stus +# 查询全部学生信息 + + +def read_all_stus(): + stus = read_stu_csv() + display(stus) + +read_all_stus() + +# 通过姓名查询某个学生信息 + +def read_someone_by_name(): + stus = read_stu_csv() + display(stus) + stu_names = stus['姓名'].values.tolist() + + 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 + +read_someone_by_name() + + +# 通过电话查询某个学生信息 + +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 + +read_someone_by_phone() +# 通过姓名/电话查询某个学生信息 + +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 +read_stu() + +# 增加某个学生信息 +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: + stu_name_flag = False + # 添加学生电话 + 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 + +add_stu() + +#通过姓名修改某个学生姓名 +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 + + +update_by_phone() +# 更改学生信息 + +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 +# + +update_stu() \ No newline at end of file