【问题标题】:Change variable in text file according to the different table [closed]根据不同的表格更改文本文件中的变量[关闭]
【发布时间】:2019-05-13 06:29:36
【问题描述】:

我无法通过我的毕业设计的难度。我必须计算文本文件中所有学生的 GPA。我有一个文本文件和表格。我将分享文件和表格的屏幕截图。首先,我必须更改文本文件中的字母等级(例如 A、B、C)。然后我应该计算所有这些点。我知道如何计算代码末尾的所有成绩,但我找不到用分数更改字母成绩的方法。

文本文件:https://i.resimyukle.xyz/QQbbH9.png

成绩:https://i.resimyukle.xyz/y3dOO7.png

文本文件:https://shrib.com/#lyyExJRaUnyRPTqSZ1KI

我尝试将 txt 文件导入 jupyter notebook。

我想分享我尝试过的代码,但这对我来说是一个非常新的主题,所以除了使用 pandas 将 txt 文件导入到 jupyter notebook 之外,我无法编写代码

【问题讨论】:

  • 请编辑您的问题并将studentsGrade.txt内容粘贴到问题中
  • 我添加到我的问题中
  • 您的问题应该是独立的。与外部资源的链接增加了人们甚至试图帮助您的障碍,并且经常在一段时间后停止工作。在 Stack Overflow,我们寻找对新访问者有帮助的问题,而不仅仅是快速解决您当前的问题。

标签: python jupyter


【解决方案1】:
import re
def determine_grade(score):
    if score == 4:
        return 'A'
    elif score >= 3.70:
        return 'A-'
    elif score >= 3.30:
        return "B+"
    elif score >= 3.00:
        return "B"
    elif score >= 2.70:
        return "B-"
    elif score >= 2.30:
        return "C+"
    elif score >= 2.00:
        return "C"
    elif score >= 1.70:
        return "C-"
    elif score >= 1.30:
        return "D"
    elif score > 0:
        return "D-"
    elif score == 0:
        return "F"

with open("studentsGrade.txt", "r+") as fin:
    with open("out.txt", "w+") as fout:
        for line in fin:
            numericGrade = re.findall(r'(?:\d+(?:\.\d+)?)', line)
            alphaGrade = determine_grade(float(numericGrade[1]))
            fout.write(line.replace(str(numericGrade[1]), alphaGrade))

【讨论】:

  • 您好,感谢您的回复,但我不明白我们为什么要添加 out.txt ?
猜你喜欢
  • 2022-11-05
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
相关资源
最近更新 更多