用Python pandas实现学生信息管理系统

用Python pandas实现学生信息管理系统

#!/usr/bin/python3
#Author:Qianshi
# -*- coding: utf-8 -*-
# @Time   :2020/12/23 13:53
# @Site   :system.py
# @Software:PyCharm
# 注解:开发一个系统,能够输入n个学生的基本信息,并保存在文件中;能够查询、修改、删除学生信息;

import pandas as pd

file_add = r'e:\code\test\student_imformation.xlsx'    #文件地址

def query(col,str):    #查询方法
    df = pd.read_excel(file_add)
    imfor = df.loc[df[col]==str]     #获取指定内容的行
    if imfor.empty :
        return "没有该内容 , 请重新输入"
    else:
        return imfor

def delete(str):    #删除方法
    df = pd.read_excel(file_add)
    imfor = df.loc[df['学号'] == str]  # 获取指定内容的行
    if imfor.empty :
        print("没有该内容 , 请重新输入")
    else:
        index = imfor.index
        df.drop(index,inplace=True)    #删除指定行
        df.to_excel(file_add,index=0)

def alter(num,col,str):    #修改方法
    df = pd.read_excel(file_add)
    imfor = df.loc[df['学号'] == num]  # 获取指定内容的行
    if imfor.empty:
        print("没有该内容 , 请重新输入")
    else:
        index = imfor.index
        df.loc[index,col] = str
        df.to_excel(file_add,index=0)

def add(str1,str2,str3,str4,str5):    #添加方法
    df = pd.read_excel(file_add)
    imfor = df.loc[df['学号'] == str1]  # 获取指定内容的行
    if imfor.empty:
        a = {'学号':str1,"姓名":str2,'性别':str3,'年龄':str4,'籍贯':str5}
        df = df.append(a,ignore_index=True)
        ordering(df)
        df.to_excel(file_add,index=0)
        print("添加成功!")
    else:
        print("该学号已存在,请重新输入")

def ordering(df):    #排序方法(按照学号排序)
        df.sort_values("学号", inplace=True)

def print_all():
    df = pd.read_excel(file_add)
    print(df)

while(True):
    try :
        while(True):
            print("学生系统菜单: \n 1.查询 \n 2.修改 \n 3.删除 \n 4.添加 ")
            option1 = int(input("\n 输入你要执行的选项:"))    # 输入的选项

            if(option1 == 1):    #查询功能
                option11 = int(input("\n 1.学号 2.姓名 3.性别 4.年龄 5.籍贯 6.打印所有\n 输入你要查询的选项:"))
                if(option11 == 1):    #查询学号
                    option12 = int(input("学号:"))
                    print(query('学号',option12))
                elif(option11 == 2):    #查询姓名
                    option12 = input("姓名:")
                    print(query('姓名',option12))
                elif(option11 == 3):    #查询性别
                    option12 = input("性别:")
                    print(query('性别',option12))
                elif(option11 == 4):    #查询年龄
                    option12 = int(input("年龄:"))
                    print(query('年龄', option12))
                elif(option11 == 5):    #查询籍贯
                    option12 = input("籍贯:")
                    print(query('籍贯', option12))
                elif (option11 == 6):  # 查询籍贯
                    print_all()
                else:
                    print("\n输入错误,没有该选项")

            elif(option1 == 2):    #修改功能
                option21 = int(input("输入你要修改对象的学号:"))
                option22 = int(input("\n 1.姓名 2.性别 3.年龄 4.籍贯 \n 输入你要修改的内容:"))
                if(option22 == 1):
                    option23 = input("输入修改后的姓名:")
                    alter(option21,"姓名",option23)
                elif (option22 == 2):
                    option23 = input("输入修改后的性别:")
                    alter(option21,"性别",option23)
                elif (option22 == 3):
                    option23 = int(input("输入修改后的年龄:"))
                    alter(option21,"年龄",option23)
                elif (option22 == 4):
                    option23 = input("输入修改后的籍贯:")
                    alter(option21,"籍贯",option23)
                else:
                    print("\n输入错误,没有该选项")

            elif(option1 == 3):    #删除功能
                option31 = int(input("输入你要删除对象的学号:"))
                delete(option31)

            elif (option1 == 4):  # 添加功能
                option41 = int(input("输入你要添加对象的学号:"))
                option42 = input("输入你要添加对象的姓名:")
                option43 = input("输入你要添加对象的性别:")
                option44 = int(input("输入你要添加对象的年龄:"))
                option45 = input("输入你要添加对象的籍贯:")
                add(option41,option42,option43,option44,option45)
            else:
                print("\n输入错误,没有该选项")

    except ValueError:
        print("\n输入错误,没有该选项请重新输入")

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值