【发布时间】:2022-06-15 18:37:44
【问题描述】:
我想在文件 configuration.yaml 中获得以下输出:
platform: manual
name: "AB"
code: !secret AB
arming_time: 15
delay_time: 15
trigger_time: 2
disarmed:
trigger_time: 0
armed_home:
arming_time: 0
delay_time: 0
我需要这个输出。但是,当我对带有 YAML 文件中节点的字典使用 dump() 时,我会收到以下输出(检查“代码”的值):
armed_home:
arming_time: 0
delay_time: 0
arming_time: 15
code: !!python/object:yaml.nodes.ScalarNode
end_mark: !!python/object:yaml.error.Mark
buffer: null
column: 24
index: 101
line: 5
name: ch/standart/alarm_control_panel.yaml
pointer: null
start_mark: !!python/object:yaml.error.Mark
buffer: null
column: 10
index: 87
line: 5
name: ch/standart/alarm_control_panel.yaml
pointer: null
style: null
tag: '!secret'
value: home_alarm
delay_time: 15
disarmed:
trigger_time: 0
name: Home Alarm
platform: manual
trigger_time: 2
我的代码在这里供您参考。 “秘密”是一个文件夹。也许,我需要创建一个新的 Class()。这将只转储()节点的标签和值。这样,输出将与所需的相同。
import yaml
from typing import Any, IO
import json
import sys
import os
from pathlib import Path
import ast
class Loader(yaml.SafeLoader):
"""YAML Loader with `!include` constructor."""
def __init__(self, stream: IO) -> None:
"""Initialise Loader."""
try:
self._root = os.path.split(stream.name)[0]
except AttributeError:
if AttributeError == IsADirectoryError:
print('lol')
else:
self._root = os.path.curdir
super().__init__(stream)
def construct_include(loader: Loader, node: yaml.Node) -> Any:
"""Include file referenced at node."""
try:
filename = os.path.abspath(os.path.join(loader._root, loader.construct_scalar(node)))
extension = os.path.splitext(filename)[1].lstrip('.')
return node
except IsADirectoryError:
print('lol')
yaml.add_constructor('!secret', construct_include, Loader)
p = os.path.abspath('configuration.yaml')
with open(p, 'r') as f:
data = yaml.load(f, Loader=Loader)
d = {}
for i in data:
d[i] = data[i]
file = open("configuration.yaml", "w")
yaml.dump(d, file)
【问题讨论】:
-
请添加创建类似转储的示例代码,以便我们拥有minimal reproducible example。