Say I have the list score=[1,2,3,4,5] and it gets changed whilst my program is running. How could I save it to a file so that next time the program is run I can access the c
Although the accepted answer works, you should really be using python's json
module:
import json
score=[1,2,3,4,5]
with open("file.json", 'w') as f:
# indent=2 is not needed but makes the file
# human-readable for more complicated data
json.dump(score, f, indent=2)
with open("file.json", 'r') as f:
score = json.load(f)
print(score)
The main advantages of using a json are that:
The main disadvantages of using a json are that:
hdf5
is a much better candidate for that).The json format is used for all kinds of purposes:
In general, if I want to store something I know I'm only ever going to use in the context of a python program, I typically go to pickle
. If I need a platform agnostic solution, or I need to be able to inspect my data once it's stored, I go with json
.