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()
相关推荐
ghost14315 分钟前
Python自学第1天:变量,打印,类型转化
开发语言·python·学习
depend16 分钟前
用 AI 实现一个 GBK/GB2312 转 UTF-8 工具:轻松解决文本编码转换难题(附完整源码)
python
人猿泰飞34 分钟前
在Ubuntu-22.04.5中安装ONLYOFFICE DocSpace(协作空间)【注意:安装失败,谨慎参考!】
java·linux·运维·python·ubuntu·项目管理·onlyoffice
Enougme40 分钟前
python-将文本生成音频
python·音视频·语音识别
拖拉机1 小时前
Python(八)类(下)
后端·python
晓13131 小时前
第三章 爬虫提速、selenium模块、requests模块进阶(终)
爬虫·python·selenium·测试工具·http
noravinsc2 小时前
python 使用rabbitmq
python·rabbitmq·ruby
啥都鼓捣的小yao3 小时前
实战5:Python使用循环神经网络生成诗歌
开发语言·人工智能·python·rnn·深度学习
站大爷IP3 小时前
Python中的“空”:对象的判断与比较
python
全栈派森3 小时前
Web认证宇宙漫游指南
python·node.js