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()
相关推荐
PixelBai5 小时前
JSON差异比较对比指南
json
珺毅同学5 小时前
YOLO生成预测json标签迁移问题
python·yolo·json
骑士雄师5 小时前
18.4 长期记忆可修改版
python
~小先生~6 小时前
Python从入门到放弃(一)
开发语言·python
天佑木枫6 小时前
第2天:变量与数据类型 —— 让程序记住信息
python
Dust-Chasing7 小时前
Claude Code源码剖析 - Claude Code 上下文压缩机制
人工智能·python·ai
Cloud_Shy6188 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 33 - 35)
开发语言·人工智能·笔记·python·学习方法
abcy0712139 小时前
python pandas csv异步后台清洗前端优先返回成功信息
前端·python·pandas
颜酱9 小时前
LangChain使用RAG 入门:让大模型读懂你的私有文档
python·langchain
天天进步20159 小时前
Python全栈项目--校园智能宿舍管理系统
开发语言·python