码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【Python】将json字符串配置存储为.py配置文件


    将json转换为config
    json结构为:
    在这里插入图片描述

    import json
    
    
    def convert_json_to_config(json_str):
        json_data = json.loads(json_str)
    
        config_dict = {}
        for key, value in json_data.items():
            keys = key.split(".")
            section = keys[0]
            option = ".".join(keys[1:])
            if len(keys) == 1:
                config_dict[section] = value
            else:
                if section not in config_dict:
                    config_dict[section] = {}
    
                if "." in option:
                    nested_section, nested_option = option.split(".", 1)
                    if nested_section not in config_dict[section]:
                        config_dict[section][nested_section] = {}
                    config_dict[section][nested_section][nested_option] = value
                else:
                    config_dict[section][option] = value
    
        return config_dict
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    写入到文件

    def write_config_to_file(config_dict, file_path):
        with open(file_path, "w") as file:
            for section, options in config_dict.items():
                file.write(f"{section} = {json.dumps(options, indent=4)}\n\n")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    测试

    if __name__ == "__main__":
        json_str = '{"config1":"value1","config2.subconfig1":"value2","config2.subconfig2.subconfig3":"value2", "config2.subconfig2.subconfig4":"value4","config3":"value5"}'
    
        config_dict = convert_json_to_config(json_str)
    
        write_config_to_file(config_dict, "config.py")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    结果
    在这里插入图片描述

  • 相关阅读:
    CAD绘制零件平面图
    适用于 .NET 的现代化、流畅、可测试的HTTP客户端库
    pip install 下载速度太慢
    如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
    基于Sentinel的微服务保护
    Springboot 整合 Java DL4J 实现智能客服
    三维测量与建模笔记 - 2.1 坐标转换基础
    AUTOCAD——坐标轴固定、CAD使用环形阵列绘制图形
    Windows 11 版本介绍
    2023年中国光子嫩肤行业市场规模及市场格局分析[图]
  • 原文地址:https://blog.csdn.net/qq_40205510/article/details/136565430
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号