1

I am making a game with points, and I am making a leaderboard. The problem is with my leaderboard is that I for the all time one, I don't know how to save it to the computer. So, if I start with leaderboard = {}; then it will not save it to the computer. Does anyone know how to make it to the computer?

My leaderboard:

Taylor: 56
Jeanne: 63
Karson: 48
Alex: 45
Madeline: 52

Obviously, these are just a bunch of random names, just in case of leaked info ;)

  • 2
    If your leaderboard is a dictionary as you indicate you can save using json or pickle. See [Storing Python dictionaries](https://stackoverflow.com/questions/7100125/storing-python-dictionaries) – DarrylG Mar 29 '21 at 12:49

1 Answers1

3

Use pickle module. It serializes the information before saving. Nice overview I found with gugl https://www.techcoil.com/blog/how-to-save-and-load-objects-to-and-from-file-in-python-via-facilities-from-the-pickle-module/

user1190411
  • 342
  • 1
  • 11
  • That was very helpful ( of all the answers I got, this is the best ), however, do you know how to delete it... ( I accidentally added some things there and now is on there forever lol – tha_dogg0_nub_at_code Mar 29 '21 at 15:38
  • Clarify. Do you need to erase the dump file or some keys from pickle? If the file, then rm. For keys, read the pickle, delete keys and write the pickle back. Sorry, I have a hard time assessing your skill level and command line fu :) – user1190411 Mar 29 '21 at 19:26
  • Sorry for not clarifying, I meant to say that I was testing my pickle dictionary, but I think I just found out the answer. However... If I did do leaderboard = {} in my code, it will replace my leaderboard with a blank one. How do I make sure that my leaderboard dictionary is defined while not making it back to zero or deleting my past leaderboard - Just defining it without making any changes? – tha_dogg0_nub_at_code Mar 30 '21 at 12:30
  • For one time job, just delete the file from the system. For later on, you can use file extensions, as some kind of versioning system. – user1190411 Apr 06 '21 at 13:23
  • Damn, was editing too long my comment. For one time job, just delete the file from the system. For later on, you can use file extensions, as some kind of versioning system. leaderboard.pickle as newest file, leaderboard.001, leaderboard.002 etc. as archives. You have to decide, when do you want to clear leader board. If you have a long running process, then rotate the file on start up. If you have linux and know, how to use logrotate, then you can make it magical. There's more than one way to skin a cat :D – user1190411 Apr 06 '21 at 13:29