当前位置: 首页 > news >正文

读取toml, 合并,生成新文件

依次读取三个TOML文件并合并,后续文件覆盖之前的值,最终将结果写入新文件

import toml

def deep_update(base_dict, update_dict):
    """
    递归合并字典,后续字典的值覆盖前者[6]
    """
    for key, val in update_dict.items():
        if isinstance(val, dict):
            if key in base_dict and isinstance(base_dict[key], dict):
                deep_update(base_dict[key], val)
            else:
                base_dict[key] = val.copy()
        else:
            base_dict[key] = val
    return base_dict

def merge_toml_files(file_paths, output_path):
    merged = {}
    for path in file_paths:
        with open(path, "r", encoding="utf-8") as f:
            current = toml.load(f)  # 读取TOML文件[2,7]
            deep_update(merged, current)
    
    with open(output_path, "w", encoding="utf-8") as f:
        toml.dump(merged, f)  # 写入合并后的配置[3,6]

if __name__ == "__main__":
    input_files = ["file1.toml", "file2.toml", "file3.toml"]
    output_file = "merged.toml"
    merge_toml_files(input_files, output_file)

相关文章:

  • 最新开源 TEN VAD 与 Turn Detection 让 Voice Agent 对话更拟人 | 社区来稿
  • 【C++】STL简介
  • FC7300 ADC MCAL配置引导
  • 启用rvzi可视化自己的机器人发现joint state publisher gui没有滑块
  • 数据服务共享平台方案
  • 轨道炮--范围得遍历,map巧统计
  • 蓝牙协议架构与调试工具详解(含 BLE、HCI 命令、调试命令)
  • 25年2月通信基础知识补充2:延迟对齐调制、常见卫星移动速度
  • 方法区与元空间解析
  • Reth(冗余以太网接口) 和Bridge-Aggregation(链路聚合接口)区别
  • Spring模拟转账开发
  • Python爬虫(28)Python爬虫高阶:Selenium+Splash双引擎渲染实战与性能优化
  • LangGraph 官方文档翻译 - 快速入门及示例教程(聊天、工具、记忆、人工干预、自定义状态、时间回溯)
  • 【vue】适合大型项目的封装(接口,全局字典,表格表头)
  • Python训练营打卡DAY27
  • 金属加工液展|切削液展|2025上海金属加工液展览会
  • 嵌入式开发书籍推荐
  • 云服务器的运用自如
  • GraphPad Prism项目的管理
  • 动态规划(2):问题建模与状态设计
  • 蒲慕明院士:未来数十年不是AI取代人,而是会用AI的人取代不会用的
  • 当智慧农场遇见绿色工厂:百事如何用科技留住春天的味道?
  • 金融月评|尽早增强政策力度、调整施策点
  • 总奖金池百万!澎湃与七猫非虚构写作与现实题材征文大赛征稿启动
  • 【社论】打破“隐形高墙”,让老年人更好融入社会
  • 回望星河深处,唤醒文物记忆——读《发现武王墩》