zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python之json数据存储

2023-09-11 14:19:57 时间
# 数据存储:json.dump()和json.load()
# date:2017-07-17
import json

file_name = 'D:/json_file.txt'
nums = [3, 4, 5, 7, 1, 9]
# nums = {"name": "Mike", "age": 12}
with open(file_name, 'w') as file_obj:
    '''写入json文件'''
    json.dump(nums, file_obj)
    print("写入json文件:", nums)

with open(file_name) as file_obj:
    '''读取json文件'''
    numbers = json.load(file_obj)  # 返回列表数据,也支持字典
    print("读取json文件:", numbers)

  

 运行结果:

写入json文件: [3, 4, 5, 7, 1, 9]
读取json文件: [3, 4, 5, 7, 1, 9]