python 实现文件查找和某些项输出

python 实现文件查找和某些项输出

本文是基于给定一文件(students.txt),查找其中GPA分数最高的 输出,同时输出其对应的姓名和学分

源码:

import winreg


def get_desktop():
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
                         r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')  # 利用系统的链表
    return winreg.QueryValueEx(key, "Desktop")[0]  # 返回的是Unicode类型数据


if __name__ == '__main__':
    Desktop_path = str(get_desktop())  # Unicode转化为str
    print(Desktop_path)


class Point:
    def __init__(self, name, score, gpa):
        self.name = name
        self.score = score
        self.gpa = gpa


def main():
    file = open(Desktop_path + "/student.txt")
    lines = file.readlines()
    print(lines)

    thLines = []

    for i in lines:
        element = i.split()
        line = {}
        line["name"] = element[0]
        line["score"] = element[1]
        line["gpa"] = element[2]
        thLines.append(line)
    max_gpa = 0
    index = 0
    print(thLines)
    for temp in thLines[1:]:
        temp['gpa'] = int(temp['gpa'])
        if temp['gpa'] > max_gpa:
            max_gpa = temp['gpa']
            index = temp
    print(index['name'], index['score'])


main();

你可能感兴趣的:(python)