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()
相关推荐
John Song40 分钟前
Python创建虚拟环境的方式对比与区别?
开发语言·python
geovindu42 分钟前
python: Bridge Pattern
python·设计模式·桥接模式
搞程序的心海43 分钟前
Python面试题(一):5个最常见的Python基础问题
开发语言·python
宝贝儿好4 小时前
【强化学习实战】第十一章:Gymnasium库的介绍和使用(1)、出租车游戏代码详解(Sarsa & Q learning)
人工智能·python·深度学习·算法·游戏·机器学习
程序媛一枚~7 小时前
✨✨✨使用Python,OpenCV及图片拼接生成❤️LOVE❤️字样图,每张小图加随机颜色边框,大图加随机大小随机颜色边框
图像处理·python·opencv·numpy·图像拼接
MediaTea7 小时前
Python:collections.Counter 常用函数及应用
开发语言·python
如若1237 小时前
flash-attn 安装失败?从报错到成功的完整排雷指南(CUDA 12.8 + PyTorch 2.7)
人工智能·pytorch·python
007张三丰7 小时前
知乎高赞回答爬虫:从零开始,建立你的专属知识库
爬虫·python·知识库·python爬虫·知乎·高赞回答
李昊哲小课8 小时前
Python json模块完整教程
开发语言·python·json
易醒是好梦8 小时前
Python flask demo
开发语言·python·flask