如何用yaml中的字典替换字符串

发布于 2025-02-04 02:16:14 字数 742 浏览 2 评论 0原文

我有一个看起来像这样的yaml:

- k1: /etc/prometheus/auxiliaryfiles/mac-jwt-cne
  k2: abcd
  k3: 
  k4:
    z1:
     - asfa
  k5: zxcv
  key_001:
    __TARGETS__

我有一个词典,看起来像:

{'a':[12,23,34]}

想在yaml中用上面的字典替换__ targets __

key_001:
 - a:
   - 12
   - 23
   - 34

yaml_dict = readYaml("lib/above_Yaml.yaml")
d = {'a': [12, 23, 34]}
replacements = {'__TARGETS__':[d]}

for k, v in yaml_dict.items():
    if v in replacements:
        yaml_dict[k] = replacements[v]

dumped = yaml.safe_dump(yaml_dict)
print(dumped)

我 以下错误:

     if v in replacements:
 TypeError: unhashable type: 'dict'

I have a YAML which looks like this:

- k1: /etc/prometheus/auxiliaryfiles/mac-jwt-cne
  k2: abcd
  k3: 
  k4:
    z1:
     - asfa
  k5: zxcv
  key_001:
    __TARGETS__

and I have a dictionary which looks like:

{'a':[12,23,34]}

I want to replace __TARGETS__ in YAML with the dictionary above so the resultant YAML should look like:

key_001:
 - a:
   - 12
   - 23
   - 34

My Code:

yaml_dict = readYaml("lib/above_Yaml.yaml")
d = {'a': [12, 23, 34]}
replacements = {'__TARGETS__':[d]}

for k, v in yaml_dict.items():
    if v in replacements:
        yaml_dict[k] = replacements[v]

dumped = yaml.safe_dump(yaml_dict)
print(dumped)

I get the following error:

     if v in replacements:
 TypeError: unhashable type: 'dict'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把人绕傻吧 2025-02-11 02:16:14

我将使用递归搜索并替换的方法尝试:

from __future__ import annotations
from typing import Any

import yaml

yaml_str = r"""
  k1: /etc/prometheus/auxiliaryfiles/mac-jwt-cne
  k2: abcd
  k3:
  k4:
    z1:
     - asfa
  k5: zxcv
  key_001:
    __TARGETS__
"""


def recursive_repl(o: dict | list | Any, repl_dict: dict[str, Any]):
    """Recursively search and replace ``o`` with key-value pairs in ``repl_dict``"""
    if isinstance(o, dict):
        return {k: recursive_repl(v, repl_dict) for k, v in o.items()}

    if isinstance(o, list):
        return [recursive_repl(e, repl_dict) for e in o]

    return repl_dict.get(o, o)


d = {'a': [12, 23, 34]}
replacements = {'__TARGETS__': [d]}

# load yaml string into a dict
yaml_dict = yaml.safe_load(yaml_str)

# recursively search and replace with desired replacements
yaml_dict = recursive_repl(yaml_dict, replacements)

# convert back to a yaml string, and optionally write to file
dumped = yaml.safe_dump(yaml_dict)
print(dumped)

out:out:

k1: /etc/prometheus/auxiliaryfiles/mac-jwt-cne
k2: abcd
k3: null
k4:
  z1:
  - asfa
k5: zxcv
key_001:
- a:
  - 12
  - 23
  - 34

注意:这需要 pyyaml

pip install PyYAML

I would try with an approach using a recursive search and replace, like this:

from __future__ import annotations
from typing import Any

import yaml

yaml_str = r"""
  k1: /etc/prometheus/auxiliaryfiles/mac-jwt-cne
  k2: abcd
  k3:
  k4:
    z1:
     - asfa
  k5: zxcv
  key_001:
    __TARGETS__
"""


def recursive_repl(o: dict | list | Any, repl_dict: dict[str, Any]):
    """Recursively search and replace ``o`` with key-value pairs in ``repl_dict``"""
    if isinstance(o, dict):
        return {k: recursive_repl(v, repl_dict) for k, v in o.items()}

    if isinstance(o, list):
        return [recursive_repl(e, repl_dict) for e in o]

    return repl_dict.get(o, o)


d = {'a': [12, 23, 34]}
replacements = {'__TARGETS__': [d]}

# load yaml string into a dict
yaml_dict = yaml.safe_load(yaml_str)

# recursively search and replace with desired replacements
yaml_dict = recursive_repl(yaml_dict, replacements)

# convert back to a yaml string, and optionally write to file
dumped = yaml.safe_dump(yaml_dict)
print(dumped)

Out:

k1: /etc/prometheus/auxiliaryfiles/mac-jwt-cne
k2: abcd
k3: null
k4:
  z1:
  - asfa
k5: zxcv
key_001:
- a:
  - 12
  - 23
  - 34

Note: this requires pyyaml

pip install PyYAML
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文