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()
相关推荐
凌不了云16 分钟前
windows环境下安装python第三方包
开发语言·python
大熊程序猿17 分钟前
python 读取excel数据存储到mysql
数据库·python·mysql
生椰拿铁You20 分钟前
Python
python
鸽芷咕22 分钟前
【Python报错已解决】python setup.py bdist_wheel did not run successfully.
开发语言·python·机器学习·bug
清纯世纪1 小时前
基于深度学习的图像分类或识别系统(含全套项目+PyQt5界面)
开发语言·python·深度学习
孤华暗香1 小时前
Python快速入门 —— 第三节:类与对象
开发语言·python
didiplus1 小时前
【趣学Python算法100例】百钱百鸡
python·算法·百钱百鸡
pzx_0011 小时前
【内积】内积计算公式及物理意义
数据结构·python·opencv·算法·线性回归
一丝晨光1 小时前
逻辑运算符
java·c++·python·kotlin·c#·c·逻辑运算符
ForRunner1231 小时前
使用 Python 高分解决 reCAPTCHA v3 的指南
数据库·python·microsoft