Ich versuche, ein Python -Skript zu schreiben, um eine YAML -Datei zu lesen, bestimmte Felder zu ändern und dann umzuschreiben. Ich habe jedoch ein Problem mit Multiline -Feldern. Die ursprüngliche YAML -Datei enthält Multiline -Zeichenfolgen, aber nach dem Ausführen meines Skripts ändert sich die Formatierung und ich sehe entkommene Zeichen anstelle des erwarteten Formats. p>
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")
< /code>
Original YAML: < /p>
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.
< /code>
yaml nach Ausführung des Skripts: < /p>
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:
email:
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.
'
< /code>
Ich vermute, dass das Problem während der Umwandlung zwischen YAML und dem entsprechenden Objekt /Wörterbuch auftritt. Anstatt die Datei direkt als Textdatei zu öffnen und eine einfache Ersatzoperation durchzuführen (z. B. Ersetzen ("Originaltext", "neuer Text")
), würde ich es vorziehen, eine robustere Lösung zu finden
Wie kann ich das Skript ändern, um die ursprüngliche Formatierung von Multilinfeldern in der YAML -Datei zu verwalten? < /p>
Ich versuche, ein Python -Skript zu schreiben, um eine YAML -Datei zu lesen, bestimmte Felder zu ändern und dann umzuschreiben. Ich habe jedoch ein Problem mit Multiline -Feldern. Die ursprüngliche YAML -Datei enthält Multiline -Zeichenfolgen, aber nach dem Ausführen meines Skripts ändert sich die Formatierung und ich sehe entkommene Zeichen anstelle des erwarteten Formats. p> [code]import json import sys import yaml import logging from datetime import datetime
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") < /code> Original YAML: < /p> 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.
< /code> yaml nach Ausführung des Skripts: < /p> 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: email: 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.
' < /code> Ich vermute, dass das Problem während der Umwandlung zwischen YAML und dem entsprechenden Objekt /Wörterbuch auftritt. Anstatt die Datei direkt als Textdatei zu öffnen und eine einfache Ersatzoperation durchzuführen (z. B. Ersetzen ("Originaltext", "neuer Text") [/code]), würde ich es vorziehen, eine robustere Lösung zu finden Wie kann ich das Skript ändern, um die ursprüngliche Formatierung von Multilinfeldern in der YAML -Datei zu verwalten? < /p>
Ich versuche eine Yamlconfig-Klasse mit Yaml-CPP zu machen. Eines seiner Hauptmerkmale ist, dass ihre Benutzer im Stil von Bukkit, einer Minecaft -API, unterschiedliche Knoten in einem Baum von...
Ich verwende das Google Apps -Skript, um Daten von Google Sheets in Google Slides einzugeben. Ich möchte, dass der Wert und die Formatierung, die in Google Sheets angezeigt wird, auf Google Slides...
Ich habe einen E -Mail- oder Blog -Beitrag, der HTML mit Inline -Elementen wie , , und benutzerdefinierte Attribute enthält. Im Moment sende ich die gesamte HTML direkt an die API -Übersetzung von...
Ich habe einen E -Mail- oder Blog -Beitrag, der HTML mit Inline -Elementen wie , , und benutzerdefinierte Attribute enthält. Im Moment sende ich die gesamte HTML direkt an die API -Übersetzung von...