用python的字典写一个实用的学生信息管理系统

一个适合新手研究的管理信息系统,脱去繁琐,功能绝对实用。
废话不多上直接上代码:

import json
import time
print('欢迎进入管理信息系统')

class Manegement():
    def add():
        with open('info.txt','r',encoding='utf-8') as f:
            a = eval(f.read())

            # create dictionary
            info = a
            names = {}

            # make some value to info
            name = input('姓名:')
            number = input('学号:')
            age = input('年龄:')
            phone = input('电话:')
            sex = input('性别:')

            names['姓名'] = name
            names['学号'] = number
            names['年龄'] = age
            names['电话'] = phone
            names['性别'] = sex
            info.setdefault(name,names)

            # write in a text file
            with open('info.txt','w',encoding='utf-8') as fr:
                fr.write(str(info))

    def index():
        with open('info.txt','r',encoding='utf-8') as f:
            a = eval(f.read())
            nm = input('请输入查找人姓名:')
            # print(a[nm])
            print('-----------------------------学生信息-----------------------------')
            print('| 姓名     |    学号      |    年龄      |      电话      |   性别  |')
            print('-----------------------------------------------------------------')
            print('{}        {}         {}          {}      {}'.format(a[nm]['姓名'],a[nm]['学号'],a[nm]['年龄'],a[nm]['电话'],a[nm]['性别']))
            print('-----------------------------------------------------------------')

    def redata():
        with open('info.txt','r',encoding='utf-8') as f:
            a = eval(f.read())

            # create dictionary
            info = a
            names = {}
            change = {}
            # make some value to info
            name = input('姓名:')
            number = input('学号:')
            age = input('年龄:')
            phone = input('电话:')
            sex = input('性别:')

            names['姓名'] = name
            names['学号'] = number
            names['年龄'] = age
            names['电话'] = phone
            names['性别'] = sex
            change.setdefault(name,names)
            info.update(change)

            # write in a text file
            with open('info.txt','w',encoding='utf-8') as fr:
                fr.write(str(info))

    def delete():
        with open('info.txt','r',encoding='utf-8') as f:
            a = eval(f.read())
            info = a
            nm = input('请输入要删除的姓名:')
            info.pop(nm)
            with open('info.txt','w',encoding='utf-8') as fr:
                fr.write(str(info))
                time.sleep(2)
                print('删除完成!!!')
                time.sleep(2)

    def index_all():
        with open('info.txt','r',encoding='utf-8') as f:
            a = eval(f.read())
            info = a
            info.values()
            b = eval((str(info.values()).replace('dict_values(','')).replace(')',''))
            print('-----------------------------学生信息-----------------------------')
            print('| 姓名     |    学号      |    年龄      |      电话      |   性别  |')
            print('-----------------------------------------------------------------')
            for i in b:
                # print(i)
                print('{}        {}         {}          {}      {}'.format(i['姓名'], i['学号'], i['年龄'], i['电话'], i['性别']))
            print('-----------------------------------------------------------------')

    def output():
        with open('info.txt','r',encoding='utf-8') as f:
            a = eval(f.read())
            info = a
            info.values()
            b = eval((str(info.values()).replace('dict_values(','')).replace(')',''))
            print('正在导出学生信息~~~~~')
            for i in b:
                # print(i)
                out_info = i['姓名']+','+str(i['学号'])+','+i['年龄']+','+str(i['电话'])+','+i['性别']+'\n'
                with open('info.csv', 'a+', encoding='gbk') as fp:
                    fp.write(out_info)


while True:
    if __name__=="__main__":
        a = Manegement
        print('+------------------------------------------+')
        print('|           1、添加指定学生信息               |')
        print('|           2、查看指定学生信息               |')
        print('|           3、修改指定学生信息               |')
        print('|           4、删除指定用户信息               |')
        print('|           5、查看所有用户信息               |')
        print('|           6、导出所有用户信息               |')
        print('+------------------------------------------+')
        chosen = int(input('请选择:'))
        # add students information
        if chosen == 1:
            a.add()
        # index students information
        elif chosen == 2:
            a.index()
        # update students information
        elif chosen == 3:
            a.redata()
        # delete students information
        elif chosen == 4:
            a.delete()
        # delete all students information
        elif chosen == 5:
            a.index_all()
        # output all information to csv
        elif chosen == 6:
            a.output()

        else:
            time.sleep(2)
            print('您的选择有误,请重新选择!!!')
            time.sleep(3)

喜欢的朋友可以给个关注。

你可能感兴趣的:(python,编程)