-2

I have a below yml file samplelambda1.yml

Mappings:
  Environments:
    dev:
        DefaultLambdaConcurrency: '10'
    test:
        DefaultLambdaConcurrency: '10'
    staging:
        DefaultLambdaConcurrency: '10'
    production:
        DefaultLambdaConcurrency: '10'

I have multiple files like samplelambda2,3,4....30

I need to add another mapping to all the yml file with help of python script, how can we do it? I tried string replace but its a complicated operation to open a file and replace multiple lines.

production-new:
    DefaultLambdaConcurrency: '10'
t.niese
  • 39,256
  • 9
  • 74
  • 101
Bhavik Joshi
  • 2,557
  • 6
  • 24
  • 48
  • The question appears to be "how do I properly handle yaml files in Python?" and the answer is that there isn't built-in standard library support and you would be better off with a third-party library rather than trying to write it yourself, because it is a standard format with a lot of established support (but hasn't yet made it into the standard library). Requests for such are off topic for Stack Overflow. I suggest you try [an internet search](https://duckduckgo.com/?q=python+yaml). This [should have been your first step anyway](https://meta.stackoverflow.com/q/261592/). – Karl Knechtel Sep 16 '21 at 06:37

3 Answers3

0

You can handle yaml files in python with pyyaml. To add to the existing yaml you could do something like:

import yaml

with open("file_append.yaml", "r") as fs:
        append_dict = yaml.load(fs, Loader=yaml.SafeLoader)

append_to = ["file_1.yaml"]

for file_name in append_to:
    with open(file_name, "r") as fs:
            target_dict = yaml.load(fs, Loader=yaml.SafeLoader)

    target_dict["Mappings"]["Environments"] = {**target_dict["Mappings"]["Environments"], **append_dict}

    with open(file_name, "w") as fs:
            yaml.dump(target_dict, fs)

Output:

Mappings:
  Environments:
    dev:
      DefaultLambdaConcurrency: '10'
    production:
      DefaultLambdaConcurrency: '10'
    production-new:
      DefaultLambdaConcurrency: '10'
    staging:
      DefaultLambdaConcurrency: '10'
    test:
      DefaultLambdaConcurrency: '10'
Tzane
  • 2,752
  • 1
  • 10
  • 21
0

Below code works for me thanks to @kaushal

for multiple file I am iterating the code in a for loop

data={}

with open("test.yml") as ymlfile:
    data = yaml.load(ymlfile, Loader=yaml.FullLoader)
    print(data)

data['Mappings']['Environments']['production-new'] = {'DefaultLambdaConcurrency': '10'}

print(data)

with open("test.yml",'w') as ymlfile:
    dumpdata = yaml.dump(data)
    ymlfile.write(dumpdata)
Bhavik Joshi
  • 2,557
  • 6
  • 24
  • 48
-1

The question shows no effort (no offence but OP should try his approach and post wherever he's stuck) so here is just the hint.

import os

for filename in os.listdir(directory):
    // read each YAML using this: https://stackoverflow.com/a/1774043/5353128
    // add your key value in the read YAML dict.
    // Use yaml.dump() to save the content back to file.
Kaushal28
  • 5,377
  • 5
  • 41
  • 72