forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaving_json.py
More file actions
30 lines (24 loc) · 687 Bytes
/
saving_json.py
File metadata and controls
30 lines (24 loc) · 687 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
import json
# example dictionary to save as JSON
data = {
"first_name": "John",
"last_name": "Doe",
"email": "john@doe.com",
"salary": 1499.9, # just to demonstrate we can use floats as well
"age": 17,
"is_real": False, # also booleans!
"titles": ["The Unknown", "Anonymous"] # also lists!
}
# save JSON file
# 1st option
with open("data1.json", "w") as f:
json.dump(data, f)
# 2nd option
with open("data2.json", "w") as f:
f.write(json.dumps(data, indent=4))
unicode_data = {
"first_name": "أحمد",
"last_name": "علي"
}
with open("data_unicode.json", "w", encoding="utf-8") as f:
json.dump(unicode_data, f, ensure_ascii=False)