[Python] Issue with YAML formatting when modifying multiline fields in Python script

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to write a Python script to read a YAML file, modify certain fields, and then rewrite it. However, I'm facing an issue with multiline fields. The original YAML file contains multiline strings, but after running my script, the formatting changes, and I see escaped characters instead of the expected format.

    Here's the code I'm using:

    import json
    import sys
    import yaml
    import logging
    from datetime import datetime

    # Configure logging
    logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

    def update_yaml_files(files, field, author_name, author_email):
    logging.info("Starting update_yaml_files function")
    files_path = json.loads(files)
    logging.info(f"Parsed JSON input: {files_path}")
    for file in files_path:
    logging.info(f"Processing file: {file}")
    try:
    with open(file, 'r', encoding='utf-8') as f:
    data = yaml.load(f, Loader=yaml.FullLoader)
    logging.info(f"Loaded YAML data from {file}: {data}")
    data['approvals'][field]['name'] = author_name
    data['approvals'][field]['email'] = author_email
    logging.info(f"Updated name and email for field '{field}'")
    if 'approvaldate' in data['approvals'][field]:
    data['approvals'][field]['approvaldate'] = datetime.now().strftime("%d-%b-%Y")
    logging.info(f"Updated approval date for field '{field}'")
    print(f"Updated data with approval date for {file}: {data}")
    with open(file, 'w', encoding='utf-8') as f:
    yaml.safe_dump(data, f, default_flow_style=False, sort_keys=False, allow_unicode=True, indent=2, width=4096)
    logging.info(f"Successfully wrote updated data to {file}")
    except Exception as e:
    logging.error(f"Error processing file {file}: {e}")

    if __name__ == "__main__":
    if len(sys.argv) != 5:
    logging.error("Invalid number of arguments. Expected 4 arguments.")
    sys.exit(1)
    files = sys.argv[1]
    field = sys.argv[2]
    author_name = sys.argv[3]
    author_email = sys.argv[4]
    logging.info("Script started with arguments: "f"files={files}, field={field}, author_name={author_name}, author_email={author_email}")
    update_yaml_files(files, field, author_name, author_email)
    logging.info("Script finished")


    Original YAML:

    type: Microsoft.KeyVault/vaults
    apiVersion: 2022-07-01
    name: Azure MS KeyVault Definition
    version: 1.0.0
    servicename: keyvault
    description: This document describes about the configuration management standard for Azure Key Vault
    targetcsp: azure
    approvals:
    author:
    name: msbauthorname
    email: msbauthoremail
    approver:
    name: msbapprovername
    email: msbapproveremail
    approvaldate: approvaldate
    reviewer:
    name: msbreviewername
    email: msbrevieweremail
    approvaldate: reviewdate
    assumptions: |
    - The version of Azure Key Vault is that of the time of writing as of October 2024
    - Users of this guide have a working knowledge of Azure, Azure Key Vault, basic network knowledge of TCP/IP, and basic system administration skills. In addition, some familiarity with Microsoft PowerShell is important for those deploying the service to the cloud.
    - The settings outlined in this document are thoroughly tested before installing them on an operational production network.
    control:
    control1:
    validation: |
    To check the SKU for an Azure Key Vault using the Azure Portal, you can follow these steps:
    1. **Sign In to Azure Portal**:
    - Go to the [Azure Portal](https://portal.azure.com) and sign in with your Azure account credentials.
    2. **Navigate to Key Vaults**:
    - On the left-hand side menu, click on "All services."
    - In the "All services" search box, type in "Key Vaults" and select it from the dropdown menu.
    3. **Select Your Key Vault**:
    - From the list of Key Vaults, click on the name of the Key Vault that you want to upgrade.
    4. **Check the Pricing Tier**:
    - Once inside the selected Key Vault, find the "Sku (Pricing tier)" under the "Essentials" section in the "Overview" panel.
    remediation: |
    Azure Key Vault's pricing tier (SKU) can't be changed directly through the Azure Portal's user interface after the Key Vault has been created.



    YAML after running the script:

    type: Microsoft.KeyVault/vaults
    apiVersion: 2022-07-01
    name: Azure MS KeyVault Definition
    version: 1.0.0
    servicename: keyvault
    description: This document describes about the configuration management standard for Azure Key Vault
    targetcsp: azure
    approvals:
    author:
    name: <author name>
    email: <author e-mail>
    approver:
    name: msbapprovername
    email: msbapproveremail
    approvaldate: approvaldate
    reviewer:
    name: msbreviewername
    email: msbrevieweremail
    approvaldate: reviewdate
    assumptions: '- The version of Azure Key Vault is that of the time of writing as of October 2024
    - Users of this guide have a working knowledge of Azure, Azure Key Vault, basic network knowledge of TCP/IP, and basic system administration skills. In addition, some familiarity with Microsoft PowerShell is important for those deploying the service to the cloud.
    - The settings outlined in this document are thoroughly tested before installing them on an operational production network.'
    controls:
    control1:
    validation: "To check the SKU for an Azure Key Vault using the Azure Portal, you can follow these steps:\n1. **Sign In to Azure Portal**:\n - Go to the [Azure Portal](https://portal.azure.com) and sign in with your Azure account credentials.\n2. **Navigate to Key Vaults**:\n - On the left-hand side menu, click on \"All services.\"\n - In the \"All services\" search box, type in \"Key Vaults\" and select it from the dropdown menu.\n3. **Select Your Key Vault**:\n - From the list of Key Vaults, click on the name of the Key Vault that you want to upgrade.\n4. **Check the Pricing Tier**:\n - Once inside the selected Key Vault, find the \"Sku (Pricing tier)\" under the \"Essentials\" section in the \"Overview\" panel.\n"
    remediation: 'Azure Key Vault''s pricing tier (SKU) can''t be changed directly through the Azure Portal''s user interface after the Key Vault has been created.

    '


    I suspect that the problem arises during the conversion between YAML and the corresponding object / dictionary. Instead of directly opening the file as a text file and performing a simple replace operation (e.g., replace("original text", "new text")), I would prefer to find a more robust solution

    How can I modify the script to maintain the original formatting of multiline fields in the YAML file?

    Continue reading...

Compartilhe esta Página