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()
相关推荐
Java后端的Ai之路7 小时前
【Python教程02】-列表和元组
服务器·数据库·python·列表·元组
好好学仿真7 小时前
探索超表面智能设计:当FDTD仿真遇上Python优化
python·联合仿真·机器学习算法·光学·fdtd·超表面逆向设计·超表面器件设计
沈浩(种子思维作者)7 小时前
量子AI真的可以在经典物理硬件中实现吗?
人工智能·python·量子计算
走遍西兰花.jpg7 小时前
修改jupyter 的默认路径
python·jupyter
errorPage7 小时前
Python空值判断避坑指南 + 图片定点缩放逻辑优化实战
python
郝学胜-神的一滴8 小时前
Python方法类型详解:类方法、静态方法与实例方法
开发语言·python·程序人生
百***24378 小时前
Grok-4.1 API进阶实战:Python项目集成、性能优化与异常处理全攻略
python·spring·性能优化
Trust yourself2438 小时前
魔塔社区下载的大模型如何通过ollama部署到本地
python
码农胖虎-java8 小时前
【java并发编程】从源码角度彻底理解 ForkJoinPool.commonPool
java·开发语言·python
毕设源码-朱学姐8 小时前
【开题答辩全过程】以 基于Python淘宝电脑销售数据可视化系为例,包含答辩的问题和答案
python·信息可视化·电脑