我正在制作像太空入侵者这样的游戏。 游戏将从游戏中获取所有分数,对其进行排序,然后将分数保存到csv文件中。 我这样做的方法是将分数保存到未排序的csv文件,读取和排序这些值,然后将排序的值保存到不同的排序的csv文件。 但是,我希望我的csv文件只存储前10个分数。 这是我目前用于此过程的代码:
if col: with open("rec_Scores.csv", "ab") as f: #adding new score into unsorted file w = csv.writer(f, delimiter = ",") w.writerow([curr_score]) with open ("rec_Scores.csv", "rb") as csvfile: #reads file, converts values to integers and sorts file (including the new score) r = csv.reader(csvfile) scores = list(r) scores_int = [int(score[0]) for score in scores] bubbleSort(scores_int) scores_int.pop() print(scores_int) with open("srt_Scores.csv", "wb") as sortfile: #Should write a new csv file every time containing the sorted scores w = csv.writer(sortfile, delimiter = ",") w.writerows([[v] for v in scores_int])我知道这不是执行此过程的最有效方法,但它主要起作用。 如果有人可以帮助我让程序只存储前10个分数,那将非常感激。
Ps:这是我第一次使用Python中的csv文件,所以我知道它是混乱和低效的,但分数存储和排序很好,我只想限制值的数量。
I am currently making a game like space invaders. The game will take in all the scores from the game, sort them, and then save the scores to a csv file. My method of doing this is by saving scores to an unsorted csv file, reading and sorting those values, then saving the sorted values to different sorted csv file. However, I want my csv file to only store the top 10 scores. This is the code that I currently have for this process:
if col: with open("rec_Scores.csv", "ab") as f: #adding new score into unsorted file w = csv.writer(f, delimiter = ",") w.writerow([curr_score]) with open ("rec_Scores.csv", "rb") as csvfile: #reads file, converts values to integers and sorts file (including the new score) r = csv.reader(csvfile) scores = list(r) scores_int = [int(score[0]) for score in scores] bubbleSort(scores_int) scores_int.pop() print(scores_int) with open("srt_Scores.csv", "wb") as sortfile: #Should write a new csv file every time containing the sorted scores w = csv.writer(sortfile, delimiter = ",") w.writerows([[v] for v in scores_int])I know this isn't the most efficient method to do this process but it works mostly. If anyone could help me make the program just store the top 10 scores it would be much appreciated.
P.s: This is my first time working with a csv file in Python so I know it is messy and inefficient, but the scores are stored and sorted fine, I just want to limit the number of values.
最满意答案
如果上面的代码对你来说是“正常工作”(你可以添加新的分数,阅读并对它们进行排序),那么要将它们限制为10,你需要做的就是切掉前10个分数。 替换此行:
scores_int.pop()...有了这个:
scores_int = scores_int[:10]假设您的最佳分数排在第一位。 如果没有,那么你想要最后 10个项目:
scores_int = scores_int[-10:]在列表中有10个项目之前执行此操作是安全的。 在这种情况下,任何给定的行都不会做任何事情。
If the above code is otherwise "working" for you (you're able to add new scores, read and sort them), then to limit them to 10 all you need to do is slice off the first 10 scores. Replace this line:
scores_int.pop()...with this:
scores_int = scores_int[:10]That assumes your best scores come first. If not, then you want the last 10 items instead:
scores_int = scores_int[-10:]It's safe to do this before you have 10 items in the list. In that case either of the given lines will simply do nothing.
更多推荐
发布评论