代码之家  ›  专栏  ›  技术社区  ›  c0d3rbox

如何在Python脚本中加载yaml文件?

  •  0
  • c0d3rbox  · 技术社区  · 1 年前

    我试图克隆一个远程仓库,打开一个特定的yaml文件,更新几个密钥,然后暂存并将其推回远程。但我无法暂存或提交文件,因为我的脚本似乎忽略了我的更新。

    我加载文件并将要更新的密钥添加到 update_values 变量

    yaml_file_path = "build.yml"
    try:
    
        with open(yaml_file_path, 'r') as file:
            data = yaml.safe_load(file)
    
    
        update_values = {
            'key1': 'q700mb',
            'key2': 'xyz',
        }
    

    然后尝试将更新“写”回其中

        for key, value in update_values.items():
            if key in data:
                data[key] = value
    
        
        with open(yaml_file_path, 'w') as file:
            yaml.dump(data, file)
    
    except Exception as e:
        print("error when touching the YAML file: {e}")
    

    我的提交失败,说明没有什么需要更新的。然后我打开并记录我希望更新的文件的内容,只是为了检查

    print("updated yaml file: ")
    with open(yaml_file_path,'r') as file:
        updated_yaml = file.read()
        print(updated_yaml)
    

    但一切都没有改变

    0 回复  |  直到 1 年前