-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjson_write.py
More file actions
49 lines (40 loc) · 1.19 KB
/
json_write.py
File metadata and controls
49 lines (40 loc) · 1.19 KB
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
41
42
43
44
45
46
47
48
49
"""
Shows how to write to a JSON data file using the json module's dumps() function.
"""
import json
def main():
"""
A simple method to write to a JSON data file using the json module's dumps() function
"""
# a list of dictionaries, containing our data
works = [
{
"Last Name": "Carroll",
"First Name": "Lewis",
"Title": "Jabberwocky",
"Year": 1871,
},
{
"Last Name": "Lear",
"First Name": "Edward",
"Title": "The Jumblies",
"Year": 1910,
},
{
"Last Name": "Bishop",
"First Name": "Elizabeth",
"Title": "The Man-Moth",
"Year": 1946,
},
]
# create proper JSON data from this list of dictionaries, indent it nicely
json_data = json.dumps(works, indent=2)
# write that JSON data to the file
f = open("data/nonsense_literature.json", "w")
f.write(json_data)
# when done, always close the file
f.close()
# ------------------------------------------------------------ #
# If this file is being run directly, call the main method ... #
if __name__ == "__main__":
main()