数据存储和读取

使用json.dump()和json.load()

存储数据

1
2
3
4
5
6
7
import  json

numbers = [2, 3, 5, 7, 11, 12]

filename = "numbers.json"
with open(filename, "w") as file_object:
json.dump(numbers, file_object)

读取数据

1
2
3
4
5
6
7
import json

filename = "numbers.json"

with open(filename, "r") as file_object:
numbers = json.load(file_object)
print(numbers)