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

时间:2019-05-11 23:15:59

标签: python regex string replace yaml

我正在尝试将特定字段中的某些值从一个YAML文件替换为另一个。

我有2种不同的文件夹结构。

├── 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

├── yaml -> root folder
    ├── accounting-pl.yaml
    ├── bi-client-svc.yaml
    ├── bi-cltscreen.yaml
    ├── bi-cnsm-lispl.yaml
    ├── bi-cnsm-unpl.yaml
    ├── bi-cons-unpl.yaml
    ├── bi-consumers.yaml
    ├── bi-data-svc.yaml
    ├── bi-datareten.yaml
    ├── clapserver.yaml
    ├── client.yaml
    ├── cn-p24.yaml
    ├── cn-payu.yaml
    ├── consent.yaml
    ├── debt-coll-pl.yaml
    ├── document.yaml
    ├── ds-falcon-bare.yaml
    ├── ds-slackme.yaml
    ├── ds-ss-m0563c0.yaml
    ├── ds-ss-m18d452.yaml
    ├── ds-ss-m1d681c.yaml
    ├── ds-ss-m30b4b3.yaml
    ├── ds-ss-m459e80.yaml
    ├── ds-ss-m48a063.yaml
    ├── ds-ss-m50eacc.yaml
    ├── ds-ss-m57043d.yaml
    ├── ds-ss-m5fb04b.yaml
    ├── ds-ss-m69bc21.yaml
    ├── ds-ss-m6edd4b.yaml
    ├── ds-ss-m706b21.yaml
    ├── ds-ss-m7fb269.yaml
    ├── ds-ss-m802936.yaml
    ├── ds-ss-m8aa1c3.yaml

在values.yaml文件中包含字段replicaCount: 1 在每个ds-ss-m7fb269.yaml这样的YAML文件中(在yaml根文件夹中),都有字段desiredCount: x

如何获取此字段desiredCount的值并粘贴到replicaCount中?例如。 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...

如何替换这些值?

0 个答案:

没有答案
相关问题