Een dictionary bestaat alleen in het geheugen. Met JSON kun je een dictionary opslaan in een bestand:
Opslaan:
import json
spel_info = {"goud": 100, "zilver": 50, "zwaard": True}
uitvoer_bestand = open("spelstand.json", "w")
json.dump(spel_info, uitvoer_bestand)
uitvoer_bestand.close()
Laden:
import json
invoer_bestand = open("spelstand.json", "r")
spel_info = json.load(invoer_bestand)
invoer_bestand.close()
print(spel_info)