Use json.dump () to store a list of numbers:
import json
numbers = [2, 3, 5, 7, 11, 13]
filename = 'numbers1.json'
with open(filename, 'w') as file_object:
json.dump(numbers, file_object)
We import the module first json
, and then create a list of numbers. We specify that you want to store the list of numbers to which
the name of the file. Typically use the file extension .json
to indicate the data file storage for the JSON
format. Next, we
open the file in write mode, so that json
can write data therein. We use the function json.dump()
to a list of numbers stored in the file numbers1.json
in.
Use json.load () will read this list into memory:
import json
filename = 'numbers1.json'
with open(filename) as file_object:
numbers = json.load(file_object)
print(numbers)