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()
相关推荐
yanghuashuiyue2 小时前
LangGraph框架研究-开发测试
python·langgraph
禹凕2 小时前
PyTorch——安装(有无 NVIDIA 显卡的完整配置方案)
人工智能·pytorch·python
卷心菜狗2 小时前
Python进阶--迭代器
开发语言·python
dragen_light2 小时前
5.ROS2-Topics-Publisher-Subscriber
python
jr-create(•̀⌄•́)2 小时前
LeakyRelu链式法则
开发语言·python·深度学习
vx_biyesheji00013 小时前
计算机毕业设计:Python股价预测与可视化系统 Flask框架 数据分析 可视化 机器学习 随机森林 大数据(建议收藏)✅
python·机器学习·信息可视化·数据分析·flask·课程设计
lulu12165440788 小时前
Claude Code项目大了响应慢怎么办?Subagents、Agent Teams、Git Worktree、工作流编排四种方案深度解析
java·人工智能·python·ai编程
Ares-Wang9 小时前
Flask》》 Flask-Bcrypt 哈希加密
后端·python·flask
kongba0079 小时前
项目打包 Python Flask 项目发布与打包专家 提示词V1.0
开发语言·python·flask
belldeep10 小时前
介绍 遗传算法 与 TSP问题
python·遗传算法·ga·tsp问题