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量化趋势跟踪实战 01】均线多头排列就是趋势开始吗:过滤与确认
python
用户298698530143 小时前
Python 实战:把代码写入 Word 文档的两种实现方式
后端·python·api
小小张说故事3 小时前
Python 里 pickle 到底存了什么:拆开 __reduce__、__getstate__ 和那串能执行代码的字节码
python
reasonsummer3 小时前
【教学类-150-03】20261108蝴蝶连连看html(一页2个版面、3个版面)
python·html
用户019027581613 小时前
用 Polars 快速分析 A 股 K 线:比 pandas 更快的列式处理
python
用户0332126663673 小时前
使用 Python 轻松加密或解密 Excel 文档
python·excel
CyrusYiVPP4 小时前
手写 MCP Server:给问答机器人接上本地文件工具,Python 实战第 13 期
人工智能·python
特立独行的猫A5 小时前
cj-tauri:用仓颉写桌面与鸿蒙 PC 应用
java·后端·python
qq_1588954111113 小时前
AI 音乐生成完成后,音频 URL 为什么会过期?一套可恢复的交付方案
python·api·ai音乐
code2cat14 小时前
【随笔】结构化输出:JSON格式过关,业务数据就能入库吗
json·大语言模型