如何将值从一个yaml文件替换到另一个文件?

2024-06-01 03:33:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个YAML文件替换一些特定字段的值到另一个文件。在

我有两个不同的文件夹结构。在

├── helm -> root folder
    ├── accounting-pl.yaml
    ├── Chart.yaml
        ├── charts
        ├── templates
        │   ├── NOTES.txt
        │   ├── _helpers.tpl
        │   ├── deployment.yaml
        │   ├── ingress.yaml
        │   ├── service.yaml
        │   └── tests
        │       └── test-connection.yaml
        └── values.yaml
    └── unplse
        ├── Chart.yaml
        ├── charts
        ├── templates
        │   ├── NOTES.txt
        │   ├── _helpers.tpl
        │   ├── deployment.yaml
        │   ├── ingress.yaml
        │   ├── service.yaml
        │   └── tests
        │       └── test-connection.yaml
        └── values.yaml
    ....... and so son

以及

^{2}$

在值.yaml文件中有字段replicaCount: 1 在每个YAML文件中,比如ds-ss-m7fb269.yaml(在YAML根文件夹中),有一个字段desiredCount: x

如何获得此字段desiredCount的值并粘贴到replicaCount?E、 g.replicaCount: 44

我试了很多东西,但都失败了。
这就是我目前所做的。在

def get_replicaCount():
    for root, dir, files in os.walk('.', topdown=False):
        for name in files:
            if name == "values.yaml":
                with open(os.path.join(root, name), 'rt') as helm:
                    try:
                        lyamls = yaml.safe_load(helm)
                        print(lyamls['replicaCount'])
                    except Exception as err:
                       print(err)

它打印所有replicaCount

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

……等等

def get_desiredCount():
    for i in os.listdir(path):
        with open('yaml/' + i, 'r') as f:
            loaded = yaml.safe_load(f)
            print(loaded['service']['desiredCount'])

获取所有desiredCount值并打印它们。在

4
3
9
12
and so son...

如何替换这些值?在


Tags: 文件nameinyamlforosasservice