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 分钟前
Json Dom 怎么玩转?
java·json·dom·snack4
毕设源码-钟学长33 分钟前
【开题答辩全过程】以 基于Python的健康食谱规划系统的设计与实现为例,包含答辩的问题和答案
开发语言·python
wtsolutions1 小时前
MCP Server Integration - JSON to Excel for AI and Automation
json·excel
\xin1 小时前
Fastjson 1.2.45仅JSON接口反序列化漏洞
安全·web安全·json
百***78751 小时前
Grok-4.1技术深度解析:双版本架构突破与Python API快速集成指南
大数据·python·架构
2501_942191772 小时前
基于YOLO11-HSFPN的数字检测与识别模型实现详解
python
司机20482 小时前
将virtuoso原理图信息导出到json文件
json·github
忧郁的橙子.3 小时前
26期_01_Pyhton基本语法
python
sunfove3 小时前
实战篇:用 Python 徒手实现模拟退火算法解决 TSP 问题
开发语言·python·模拟退火算法
我是菜鸟0713号4 小时前
Qt + Python 算法集成的一种低耦合实践:FastAPI 服务化方案
python·qt·fastapi