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()
相关推荐
ellenwan202633 分钟前
看到“最新 AI 量化学习”时,先让表达变清楚
人工智能·python
海宇数据1 小时前
零信任架构实战:基于海宇柠檬查出险-登记证构建自动化残值评估网关
人工智能·python·架构·自动化
李可以量化2 小时前
Tornado 如何增加网页操作(下)
python
zhangzeyuaaa2 小时前
深入理解 pip install .:安装当前目录项目的正确姿势
人工智能·python·pip
Jazz_z3 小时前
纯 Python 环境下 Excel 文件的读取与导入详解
python
Thomas.Sir3 小时前
第38课:TensorFlow|可视化工具TensorBoard全用法【日志写入、指标监控、网络可视化】
人工智能·python·tensorflow
SEO_juper3 小时前
外贸多语言站最隐蔽的流量杀手:hreflang 错了,谷歌把德语页推给美国人(附审计脚本)
开发语言·前端·python·seo·独立站·谷歌优化
Metaphor6923 小时前
快速合并 Word 文档【Python 指南】
python·word
JAVA老架构AI智能化4 小时前
如何高质量分块
人工智能·python
泡海椒4 小时前
告别反射低效:JQuick-Java ASM动态调用链性能优化实战
java·python·性能优化