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()
相关推荐
LitchiCheng11 小时前
Mujoco 检验 KDL 和 Pinocchio 运动学 FK 是否一致
人工智能·python
ZhengEnCi11 小时前
P3H1-Python-sys模块完全指南-系统参数与命令行参数处理利器
python
猫头虎11 小时前
如何解决pip install网络报错SSLError: TLSV1_ALERT_PROTOCOL_VERSION(OpenSSL过旧)问题
网络·python·scrapy·pycharm·beautifulsoup·pip·scipy
帮帮志11 小时前
05【AI大模型对话/创建项目】通过pycharm创建大模型项目,关联Anaconda环境
ide·人工智能·python·语言模型·pycharm
海边夕阳200611 小时前
【每天一个AI小知识】:什么是目标检测?
人工智能·python·深度学习·目标检测·机器学习·计算机视觉·目标跟踪
咖啡の猫11 小时前
Python分支结构
服务器·网络·python
Blossom.11811 小时前
基于Mamba-2的实时销量预测系统:如何用选择性状态空间干掉Transformer的O(n²)噩梦
人工智能·python·深度学习·react.js·机器学习·设计模式·transformer
Wise玩转AI11 小时前
Day 26|智能体的“伦理与安全边界”
人工智能·python·安全·ai·chatgpt·ai智能体
梦里不知身是客1111 小时前
帆软的图标类型介绍
python·信息可视化·数据分析
h***381811 小时前
使用python进行PostgreSQL 数据库连接
数据库·python·postgresql