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()
相关推荐
小兜全糖(xdqt)8 分钟前
python ppt转pdf以及图片提取
python·pdf·powerpoint
前端世界18 分钟前
用Python打造智能成绩分析系统:从异常处理到断言验证的全流程实战
服务器·数据库·python
yaoxin52112318 分钟前
229. Java 集合 - 操作集合中的多个元素(批量操作)
java·开发语言·python
岁岁岁平安19 分钟前
python 配置pip镜像源
python
在人间负债^21 分钟前
从Python到仓颉:核心项目内容迁移实践
开发语言·python·鸿蒙·仓颉
云和数据.ChenGuang23 分钟前
SyntaxError: Non-UTF-8 code starting
人工智能·python·numpy
站大爷IP41 分钟前
Airflow调度爬虫任务:从零搭建高效定时采集系统
python
站大爷IP1 小时前
Python高效实现Word转HTML:从基础到进阶的全流程方案
python
测试-鹏哥1 小时前
要将ITP集成到Jenkins Pipeline中,实现开发发版时自动触发自动化测试
运维·python·测试工具·ci/cd·jenkins
程序员三藏1 小时前
Postman接口测试详解
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman