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()
相关推荐
Csvn1 小时前
Python 开发技巧:标准库深度挖掘
后端·python
li星野1 小时前
二分查找的三重变奏:有序区间、旋转最小值与平方根——从“猜数字”到“向量检索”的思维演化
python
RSTJ_16252 小时前
PYTHON+AI LLM DAY ONE HUNDRED AND THREE
开发语言·人工智能·python
简~7682 小时前
python openpyxl处理Excel成绩表自动统计
python·大学生
梦想不只是梦与想2 小时前
Python 官方包管理器pip
python·pip
wahahaman3 小时前
基于GBDT的次日降水量预测实验
人工智能·python·机器学习·数据分析
Nontee3 小时前
数据类型与包装类 — 一个新手学Java的数据类型笔记
java·笔记·python
千天夜4 小时前
深入剖析 `Path(path).parent.mkdir(parents=True, exist_ok=True)` —— 优雅处理目录创建的终极方案
python
简~7684 小时前
python for in循环不能遍历整数怎么解决
python·大学生
aiqianji4 小时前
垂直专业的AI短篇小说写作软件有哪些特点?
人工智能·python