Python练习(九)——面向对象,文件操作与数据库操作

  1. 面向对象,文件操作与数据库操作复习题目:
    文件score.dat中保存的是100名学生的姓名和Python课、高数和英语成绩。

(1)定义学生类,其中包含姓名、Python课、高数和英语成绩及总分、均分数据成员,成员函数根据需要确定。

(2)读入这名学生的成绩,用对象列表进行存储。

(3)求出各科和总分的最高分。

(4)请按总分的降序(高成绩在前,低成绩在后)排序

(5)在屏幕上显示各科及总分的最高分,排序后的成绩单(包括总分)保存到文件odered_score.dat中。

(6) 将文件中的所有学生信息, 保存在mariadb数据库中;

from itertools import chain
from random import *
import pymysql

def create_student_info():
    def creat_name():
        first = ['许', '张', '赵', '钱', '孙', '李', '朱', '杨']
        second = ['彬', '群', '宁', '盼', '龙', '欢', '丹']
        last = ['彬', '群', '宁', '盼', '龙', '欢', '丹', ' ', ' ', ' ', ' ']
        name = choice(first) + choice(second) + choice(last)
        return name.rstrip()

    with open("score.dat", 'w+') as f:
        for i in range(100):
            name = creat_name()
            py_score = randint(40, 100)
            high_math_score = randint(40, 100)
            english_score = randint(40, 100)
            sum_score = int(py_score) + int(high_math_score) + int(english_score)
            ave_score = float(sum_score)
            f.write(name + ' ' + str(py_score) + ' ' + str(high_math_score) + ' ' + str(english_score) + ' '+ str(sum_score) + ' ' + str(ave_score) + '\n')

class Student(object):
    def __init__(self, name, py_score, high_math_score, english_score,sum_score,ave_score):
        self.name = name
        self.py_score = py_score
        self.high_math_score = high_math_score
        self.english_score = english_score
        self.sum_score = sum_score
        self.ave_score = float(ave_score)

    def __repr__(self):
        return '姓名:%s python成绩:%s 高数成绩:%s 英语成绩:%s 总分:%s 平均分:%.2f' %(self.name,self.py_score,
                self.high_math_score,self.english_score,self.sum_score,self.ave_score)


def main():
    #创建100个学会信息
    create_student_info()
    #读取学生信息
    with open('score.dat') as f:
        AllStudentInfo = [Student(*student_info.split(' ')) for student_info in f.readlines()]
    #输出信息
    py_score_max = max(AllStudentInfo,key=lambda x:x.py_score).py_score
    high_math_score_max = max(AllStudentInfo, key=lambda x: x.high_math_score).high_math_score
    english_score_max = max(AllStudentInfo, key=lambda x: x.english_score).english_score
    sum_score_sorted = sorted(AllStudentInfo,key=lambda x:x.sum_score,reverse=True)
    print('python最高分:%s' %(py_score_max))
    print('高数最高分:%s' % (high_math_score_max))
    print('英语最高分:%s' % (english_score_max))
    print('总分最高分:%s'  %(sum_score_sorted[0].sum_score) + '\n')
    with open('odered_score.dat','w') as f:
        for info in sum_score_sorted:
            print(info)
            f.write(str(info)+'\n')
    with open('odered_score.dat','w+') as f:
        conn = pymysql.connect(user = 'root',password = 'westos',charset = 'utf8',db ='student',autocommit = True)
        cur = conn.cursor()
        info_table = 'create table student(姓名 varchar(20) not null,python成绩 varchar(3),高数成绩 varchar(3),英语成绩 varchar(3),总分 varchar(3),平均分 varchar(5));'
        cur.execute(info_table)
        for line in f.readlines():
            studentInfo_all = [i.split(':') for i in line.split()]
            studentInfo= list(chain(*studentInfo_all))[1::2]
            insertInfo = "insert into student values('%s', '%s', '%s', '%s', '%s', '%s');" %(
                studentInfo[0], studentInfo[1], studentInfo[2], studentInfo[3], studentInfo[4], studentInfo[5])
            cur.execute(insertInfo)
        cur.close()
        conn.close()
if __name__ == '__main__':
    main()

这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41179709/article/details/82467813
今日推荐