It looks like you need a simple case of saving on to a text file. json
is probably easy to start off with.
Try this:
1. Save the contents as json
onto a text file
2. You may load the data back again and convert into json
, which will work like dict objects.
file_path = '/tmp/dict_test.txt'
import json
data = [
{"score": "10", "grade": "E", "music": "song5", "maxcombo": "1", "perfect": "20", "great": "1", "good": "20", "miss": "1"},
{"score": "20", "grade": "D", "music": "song4", "maxcombo": "2", "perfect": "20", "great": "2", "good": "20", "miss": "2"},
{"score": "30", "grade": "C", "music": "song3", "maxcombo": "3", "perfect": "20", "great": "3", "good": "20", "miss": "3"},
{"score": "40", "grade": "B", "music": "song2", "maxcombo": "4", "perfect": "20", "great": "4", "good": "20", "miss": "4"},
{"score": "50", "grade": "A", "music": "song1", "maxcombo": "5", "perfect": "20", "great": "5", "good": "20", "miss": "5"},
]
# dump the dict contents using json
with open(file_path, 'w') as outfile:
json.dump(data, outfile, indent=4, separators=(',', ':'))
# Let's read the data back again from the file
file_text = ''
with open(file_path, 'rt') as file_placeholder:
lines = file_placeholder.readlines()
file_text = ''.join(lines) # This provides the whole file data as string
print('file_text = {}'.format(file_text))
# Load as json
json_text = json.loads(file_text)
print('json = {}'.format(json_text))
print('file_text type = {}; json_text type = {}'.format(type(file_text), type(json_text)))
You will get the following results:
file_text = [
{
"good":"20",
"grade":"E",
"great":"1",
.............
}
]
json = [{'good': '20', 'grade': 'E', 'great': '1', 'music': 'song5', 'score': '10', 'miss': '1', 'maxcombo': ................ 'perfect': '20'}]
file_text type = <class 'str'>; json_text type = <class 'list'>