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()
相关推荐
Marst Code8 分钟前
(python)2026Plotly 库评估:交互式可视化到底值不值得引入?
开发语言·python
databook13 分钟前
当散点图不够用时:用 t-SNE 可视化多维数据
python·数据分析·数据可视化
我的xiaodoujiao3 小时前
快速学习Python基础知识详细图文教程9--函数进阶
开发语言·python·学习·测试工具
weixin_408099673 小时前
2026 图片去水印 API 接口完全指南:一键去除图片水印(附 Python/Java/PHP/C# 示例)
java·python·php·图片处理·api调用·图片去水印·石榴智能
去码头整点薯条ing4 小时前
某当网登录滑块【协议+OCR】
爬虫·python·ocr
赶紧写完去睡觉4 小时前
Anaconda 创建虚拟环境与使用
python·pycharm·conda
迷途呀4 小时前
Python:函数中的参数类型
开发语言·笔记·python·langchain·nlp
鱼毓屿御5 小时前
复习总结:把之前的所有 prompt 套路封装成可切换模板的 ask 函数
python
你驴我5 小时前
WhatsApp 多账号下消息已读回执的实时聚合与推送实践
后端·python
Geoking.6 小时前
JSON vs JSONL:从数据格式到 AI Agent 的工程实践
人工智能·深度学习·json