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()
相关推荐
旅途中的宽~3 分钟前
【Python】pip install -v e .命令不想自动更新torch版本
开发语言·python·pip
海棠AI实验室20 分钟前
第 3 篇:方案写作——SOW / 里程碑 / 验收标准 / 风险假设的标准模板
数据库·python
高洁0135 分钟前
AI智能体搭建(4)
python·深度学习·机器学习·transformer·知识图谱
IT=>小脑虎1 小时前
Python爬虫零基础学习知识点详解【基础版】
爬虫·python·学习
R-sz1 小时前
如何将json行政区划导入数据库,中国行政区域数据(省市区县镇乡村五级联动)
java·数据库·json
做萤石二次开发的哈哈2 小时前
萤石开放平台 萤石可编程设备 | 设备 Python SDK 使用说明
开发语言·网络·python·php·萤石云·萤石
知乎的哥廷根数学学派2 小时前
基于多物理约束融合与故障特征频率建模的滚动轴承智能退化趋势分析(Pytorch)
人工智能·pytorch·python·深度学习·算法·机器学习
HarmonLTS3 小时前
Python Socket网络通信详解
服务器·python·网络安全
郝学胜-神的一滴3 小时前
Python数据封装与私有属性:保护你的数据安全
linux·服务器·开发语言·python·程序人生
智航GIS3 小时前
11.7 使用Pandas 模块中describe()、groupby()进行简单分析
python·pandas