-1
def load():
    with open("random_number_highscores.txt","r") as x:
        print ("HIGHSCORES")
        print ("Least guesses made.")
        print (json.load(x))
        time.sleep(1)

def save(a):
    with open("random_number_highscores.txt", "a") as x:
        json.dump(a, x)
    print ("saved.")
    time.sleep(1)

Why does def load not work. Ive tried saving with json.dump(str(a), x) but it doesnt work either just get error

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • For future reference: include the actual error you are getting. I made an educated guess in this case, but the error message (including traceback) is crucial in diagnosing a problem. – Martijn Pieters Sep 01 '13 at 23:05

1 Answers1

1

You are appending to the save file, and you need to overwrite instead:

def save(a):
    with open("random_number_highscores.txt", "w") as x:
        json.dump(a, x)

The json.load() code otherwise encounters multiple JSON values and it cannot handle more than one in the file.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I dont want to overwrite the file just add to it then open it for viewing – StandingBird Sep 01 '13 at 23:08
  • @StandingBird: You can only do that when you also add newlines and when opening the file again reading it yourself line by line for processing it. See [Loading & Parsing JSON file in python](http://stackoverflow.com/a/12451465) for a solution that reads each line. – Martijn Pieters Sep 01 '13 at 23:13