From the course: Python: Working with Files
Unlock the full course today
Join today to access over 23,400 courses taught by industry experts.
Write data to a JSON file in Python - Python Tutorial
From the course: Python: Working with Files
Write data to a JSON file in Python
- [Instructor] JSON is one of the most popular formats to serialize data. After creating dictionary data with Python, you may want to save it to a JSON file. Here we start off with some sample code that will convert a given set of monster attributes into a dictionary object. Each monster product has a name, title, price, and level of scariness. We can use this function to generate a dictionary for a given monster. Let's try it. Here we create Filo who is a baker by day and techie by night. It's priced at 29 and has a scariness level of three. Let's generate a few more items. To write these dictionary objects out to a JSON file. We can use the JSON module. We'll open up a new file in write mode, and then write out the dictionary data in a JSON format. We can use the dump function to write the data in the correct format. Let's call this function with a list of our dictionary items. That's monster one, two, and…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Write data to a file in Python3m 36s
-
(Locked)
Move and rename files with Python3m 23s
-
(Locked)
Copy with Python2m 29s
-
(Locked)
Delete files with Python3m 17s
-
(Locked)
Save tabular data with csv module4m 5s
-
(Locked)
Write data to a JSON file in Python1m 48s
-
(Locked)
Solution: Rename and Reorganize Photo Files2m 24s
-
(Locked)
</> Solution: Parse JSON file with Python1m 19s
-
-
-