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()
相关推荐
sylviiiiiia24 分钟前
leetcode hot100
python·算法·leetcode
千里码aicood34 分钟前
flask-基于注意力机制的异常流量检测与自动防御系统
后端·python·flask
维克兜率天35 分钟前
4.3.2.1 日常监控:上线只是开始
服务器·数据库·python·区块链·php·量化
the局外人43 分钟前
学习 FastAPI 的 Day 2:用异步 ORM 完成增删改查
后端·python·fastapi
智搜广告1 小时前
GEO优化公司怎么选?智搜广告从三个维度帮你判断
大数据·人工智能·python·elasticsearch·microsoft·geo
wanglei2007081 小时前
软件架构设计中,各个组件之间的通信方式有哪些?
python
牛油果子哥q1 小时前
C++对接LLM完整工程:异步HTTP请求、JSON解析、超时容错、重连兜底
c++·http·json
会飞的拖把1 小时前
Python 面向对象编程详解:从类与对象到动态属性方法
开发语言·python
微小冷1 小时前
Python CasADi初步教程
python·控制·模型预测·casadi·符号表达式·sx
cpolar技术支持1 小时前
requirements.txt 能安装不等于安全:用 pip-audit 检查 Python 依赖,cpolar 临时分享脱敏报告
python·cpolar·requirements·依赖安全·pip-audit