-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_save_read.py
More file actions
40 lines (35 loc) · 827 Bytes
/
json_save_read.py
File metadata and controls
40 lines (35 loc) · 827 Bytes
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
33
34
35
36
37
38
39
40
import json
data = {}
data['people'] = []
data['people'].append({
'name': 'Scott',
'website': 'stackabuse.com',
'from': 'Nebraska'
})
data['people'].append({
'name': 'Larry',
'website': 'google.com',
'from': 'Michigan'
})
data['people'].append({
'name': 'Tim',
'website': 'apple.com',
'from': 'Alabama'
})
####### json save = dump #######
import json
def save_json(p, data):
with open(p, 'w') as outfile:
json.dump(data, outfile, indent=4)
####### json read = load #######
import json
def load_json(p):
with open(p) as json_file:
data = json.load(json_file)
return data
save_json("data.txt", data)
data = load_json("data.txt")
for p in data['people']:
print('Name: ' + p['name'])
print('Website: ' + p['website'])
print('From: ' + p['from'])