python读写json

python读写json

使用python读写json文件

python 复制代码
# -*- coding : UTF-8 -*-
# @file   : read_write_json.py
# @Time   : 2023-08-31 16:46
# @Author : wmz

import os
import json


def read_json():
    file_path = "info.json"
    with open(file_path, "r") as f:
        info = json.load(f)

    train_path = "./image"
    train_images = info['train']
    train_list = [os.path.join(train_path, p) for p in train_images]

    test_path = "./image"
    test_images = info['test']
    test_list = [os.path.join(test_path, p) for p in test_images]
    print("train_images", train_images)
    print("train_list", train_list)


def write_json():
    file_save_path = "save_info.json"
    a = {
        "train": [
            "001.nii.gz",
            "002.nii.gz",
            "004.nii.gz"
            ],
        "test": [
             "003.nii.gz",
             "007.nii.gz"
            ]
        }
    b = json.dumps(a, indent=1)
    f2 = open(file_save_path, 'w')
    f2.write(b)
    f2.close()


if __name__ == "__main__":
    write_json()
    read_json()
相关推荐
烤汉堡1 小时前
Python入门到实战:post请求+cookie+代理
爬虫·python
luod2 小时前
Python异常链
python
我不是QI2 小时前
周志华《机器学习---西瓜书》 一
人工智能·python·机器学习·ai
今天没ID2 小时前
Python 编程实战:从基础语法到算法实现 (1)
python
二川bro3 小时前
Python在AI领域应用全景:2025趋势与案例
开发语言·人工智能·python
棒棒的皮皮3 小时前
【Python】Open3d用于3D测高项目
python·3d·open3d
CodeLongBear3 小时前
Python数据分析: 数据可视化入门:Matplotlib基础操作与多坐标系实战
python·信息可视化·数据分析
李晨卓4 小时前
python学习之不同储存方式的操作方法
python·代码规范
站大爷IP4 小时前
实战:爬取某联招聘职位需求并生成词云——从零开始的完整指南
python
deephub4 小时前
从零开始:用Python和Gemini 3四步搭建你自己的AI Agent
人工智能·python·大语言模型·agent