Python从文件中读取和使用数据

Python从文件中读取和使用数据,python,Python,我目前正在尝试将测验的分数保存到excel表格中,该表格允许用户读取文件,但如何使用他们的保存数据只保存用户的最后3个分数。我理解这意味着从文件中读取分数,然后让它读取用户的尝试次数等。但我不太明白如何进行,因此程序将只保存该用户的最后3个分数或他们的3个最高分数。多谢各位 if pclass == 1: inFile = open("ascores.csv", 'a') inFile.write("\n"

我目前正在尝试将测验的分数保存到excel表格中,该表格允许用户读取文件,但如何使用他们的保存数据只保存用户的最后3个分数。我理解这意味着从文件中读取分数,然后让它读取用户的尝试次数等。但我不太明白如何进行,因此程序将只保存该用户的最后3个分数或他们的3个最高分数。多谢各位

            if pclass == 1:
                inFile = open("ascores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("ascores.csv", 'r')
                print(inFile.read())
            elif pclass == 2:
                inFile = open("bscores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("bscores.csv", 'r')
                print(inFile.read())
            elif pclass == 3:
                inFile = open("cscores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("cscores.csv", 'r')
                print(inFile.read(sorted(reader, key=lambda row: int(row[0]))))
            else:
                print("Sorry we can not save your data as the class you entered is 1, 2 or 3.")

您正在读取文件内容之前关闭文件

            if pclass == 1:
                inFile = open("ascores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("ascores.csv", 'r')
                print(inFile.read())
            elif pclass == 2:
                inFile = open("bscores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("bscores.csv", 'r')
                print(inFile.read())
            elif pclass == 3:
                inFile = open("cscores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("cscores.csv", 'r')
                print(inFile.read(sorted(reader, key=lambda row: int(row[0]))))
            else:
                print("Sorry we can not save your data as the class you entered is 1, 2 or 3.")
if pclass == 1:
    inFile = open("ascores.csv", 'a')
    inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
    content = inFile.read()
    inFile.close()
    inFile = open("ascores.csv", 'r')
    print(content)
或者更简洁地说:

            if pclass == 1:
                inFile = open("ascores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("ascores.csv", 'r')
                print(inFile.read())
            elif pclass == 2:
                inFile = open("bscores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("bscores.csv", 'r')
                print(inFile.read())
            elif pclass == 3:
                inFile = open("cscores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("cscores.csv", 'r')
                print(inFile.read(sorted(reader, key=lambda row: int(row[0]))))
            else:
                print("Sorry we can not save your data as the class you entered is 1, 2 or 3.")
if pclass == 1:
    new_score = "\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1))
    with open("ascores.csv", 'a') as in_file:
        in_file.write(new_score)
        content = in_file.read()
    print(content)
使用with语句将为您关闭文件。 您可以编辑长的新分数=\n+pname+,+strcorrect+,+strroundetime,1到:

            if pclass == 1:
                inFile = open("ascores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("ascores.csv", 'r')
                print(inFile.read())
            elif pclass == 2:
                inFile = open("bscores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("bscores.csv", 'r')
                print(inFile.read())
            elif pclass == 3:
                inFile = open("cscores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("cscores.csv", 'r')
                print(inFile.read(sorted(reader, key=lambda row: int(row[0]))))
            else:
                print("Sorry we can not save your data as the class you entered is 1, 2 or 3.")
有关字符串格式的说明,请参见

            if pclass == 1:
                inFile = open("ascores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("ascores.csv", 'r')
                print(inFile.read())
            elif pclass == 2:
                inFile = open("bscores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("bscores.csv", 'r')
                print(inFile.read())
            elif pclass == 3:
                inFile = open("cscores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("cscores.csv", 'r')
                print(inFile.read(sorted(reader, key=lambda row: int(row[0]))))
            else:
                print("Sorry we can not save your data as the class you entered is 1, 2 or 3.")
您还可以简化代码,永远不要重复:

            if pclass == 1:
                inFile = open("ascores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("ascores.csv", 'r')
                print(inFile.read())
            elif pclass == 2:
                inFile = open("bscores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("bscores.csv", 'r')
                print(inFile.read())
            elif pclass == 3:
                inFile = open("cscores.csv", 'a')
                inFile.write("\n" + pname + ", " + str(correct) + ", " + str(round(etime, 1)))
                inFile.close()
                inFile = open("cscores.csv", 'r')
                print(inFile.read(sorted(reader, key=lambda row: int(row[0]))))
            else:
                print("Sorry we can not save your data as the class you entered is 1, 2 or 3.")
def saveScore(pname, correct, etime, pclass):
    score_files = {1:"ascores.csv", 2:"bscores.csv", 3:"cscores.csv"}
    try:
        filename = score_files[pclass]
        new_score = "\n {}, {}, {:.1f}".format(pname, correct, etime)
        with open(filename, 'a') as in_file:
            in_file.write(new_score)
            content = in_file.read()         
        return content
    except KeyError:
        return None

print(saveScore(pname, correct, etime, pclass))

谢谢你,我现在只是测试一下,看看它是否同样有效。但是,我如何使用它来只允许用户将3个分数保存到文件中呢?我需要在文件中这样做,它将只保存每个用户的3个最好的分数由分数和他们得到的时间决定。最好的分数,如果他们得到2个相同的分数,那么这是由时间决定的。谢谢你的帮助。所以你现在要做的是将最后的分数附加到文件中。它不会在文件中检查用户名。我的理解是,您A:想检查文件中是否已经存在玩家姓名,B:如果是A,则将新结果附加到玩家姓名的行中,或者C:如果不是A,则添加包含玩家分数的新行?对吗?顺便问一下,你为什么用3个文件而不是1个?比如:p_名称、高分、高分、第二高分、第二高分、第三高分、第三高分