简体   繁体   English

如何从文档中删除python中的最后3个数据

[英]how to remove last 3 data in python from a doc

I've been attempting for a while to make it so that my quiz will delete data which had been done by the specific person 3 times ago.我已经尝试了一段时间,以便我的测验将删除特定人 3 次前完成的数据。 so if there is 4 people who have done the quiz and one of them is doing it for the forth time, I want it to delete the first time, this is the coding I have got so far所以如果有 4 个人做了测验,其中一个是第四次做,我希望它第一次删除,这是我目前得到的编码

its not all of it, but you can see what I am doing这不是全部,但你可以看到我在做什么

else:
    print(" ")
    print("Well Done",name,"!")
    print("Thankyou for taking this quiz")
    print("Your Score is ",finalscore,"/ 10.")
file=open(classname".txt", 'a')
file.write(str(name + " "))
file.write(str(finalscore))
file.write(str(":""\n"))
file.close()
print("Do you wish to view people's previous results?")

previousscore=input()
if previousscore=="yes".lower():
    sorter=input()
    if sorter=='1':
    reader=csv.reader(open(classname".txt"),delimiter=':')
    with open(classname".txt", 'r') as f:
        for line in sorted(f):
            print(line, end='')

I've only got it so far that it puts it into alphabetical order perfectly, but I need it to delete data.到目前为止,我只知道它完美地按字母顺序排列,但我需要它来删除数据。 The data shows up like this in the document;数据在文档中是这样显示的; Keanu 10: Keanu 10: Keanu 10: Jack 7: Jack 3: Jack 3: Harry 10: Harry 8: Jordan 7: Jordan 10: Keanu 1:基努10:基努10:基努10:杰克7:杰克3:杰克3:哈利10:哈利8:乔丹7:乔丹10:基努1:

The python coding then changes it to this;然后python编码将其更改为这个; Harry 10: Harry 8: Jack 3: Jack 3: Jack 7: Jordan 10: Jordan 7: Keanu 10: Keanu 10: Keanu 10: Keanu 1: Keanu 1:哈利10:哈利8:杰克3:杰克3:杰克7:乔丹10:乔丹7:基努10:基努10:基努10:基努1:基努1:

But I want it to look like this;但我希望它看起来像这样; Harry 10: Harry 8: Jack 3: Jack 3: Jack 7: Jordan 10: Jordan 7: Keanu 10: Keanu 10: Keanu 2:哈利10:哈利8:杰克3:杰克3:杰克7:乔丹10:乔丹7:基努10:基努10:基努2:

so it only shows the last three attempts :) any help will be so helpful, but keep in mind I have only just started python... so don't get too technical ;)所以它只显示最近的三个尝试:) 任何帮助都会很有帮助,但请记住,我才刚刚开始使用 python ......所以不要太技术性;)

This sounds a lot like a class assignment.这听起来很像课堂作业。 So my advice to you would be this:所以我给你的建议是这样的:

  1. Read the lines of text from the file.从文件中读取文本行。
  2. Determine the name (key) from the line.从行中确定名称(键)。
  3. Add the score to a dictionary of lists.将分数添加到列表字典中。 d[key] = list(score, score, score, ...)
  4. Shorten the list if it's too long.如果列表太长,请缩短列表。

After that, you'll have the "most recent" scores for each player in a list by that player's name.之后,您将获得该玩家姓名列表中每个玩家的“最近”得分。 You can sort however you like.您可以随意排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM