-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson-21-JSON.py
32 lines (30 loc) · 985 Bytes
/
Lesson-21-JSON.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import json
filename = "user_settings.txt"
myfile = open(filename, 'w', encoding='Latin-1')
player1 = {
'PlayerName': "Donald Trump",
'Score': 345,
'awards': ["OR", "NV", "NY"]
}
player2 = {
'PlayerName': "Clinton Hillary",
'Score': 346,
'awards': ["WT", "TX", "MI"]
}
myplayers = []
myplayers.append(player1)
myplayers.append(player2)
# ---------------- SAVE by JSON -------------------
json.dump(myplayers, myfile)
myfile.close()
# ---------------- LOAD by JSON -------------------
myfile = open('user_settings.txt', 'r', encoding='Latin-1')
json_data = json.load(myfile)
print(json_data)
for user in json_data:
print("Player Name is: " + str(user['PlayerName']))
print("Player Score is: "+ str(user['Score']))
print("Player Awards № 1: " + str(user['awards'][0]))
print("Player Awards № 1: " + str(user['awards'][1]))
print("Player Awards № 1: " + str(user['awards'][2]))
print("---------------------------------------------")