forked from fipl-hse/2023-2-level-ctlr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry_json.py
31 lines (23 loc) · 759 Bytes
/
try_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
# pylint: disable=unspecified-encoding
"""
Listing for practice with json module
"""
import json
def main() -> None:
"""
Entrypoint for a seminar's listing
"""
# 1. Create JSON file from a dictionary
student = {'name': 'John', 'surname': 'Davis', 'age': 15, 'hobbies': ['sport', 'reading']}
res = json.dumps(student)
with open('sample.json', 'w') as f:
f.write(res)
# 2. Write dict to JSON directly
with open('sample.json', 'w', encoding='utf-8') as f:
json.dump(student, f, ensure_ascii=True, indent=4, separators=(', ', ': '))
# 3. Read from JSON
with open('sample.json', 'r', encoding='utf-8') as f:
content = json.load(f)
print(content)
if __name__ == '__main__':
main()