-1

I'm pretty new to PyYAML and don't understand everything about it. I have a YAML file with a list;

the-list:
  - "string1"
  - "string2"
  - "string3"

I have managed to find out how to fetch the list in my python file, but not how to add another string on to it, and save it to the YAML file. So it would become

the-list:
  - "string1"
  - "string2"
  - "string3"
  - "newstring"

Help appreciated! Here's the code I use for reading the YAML file:

with open('config.yml', 'r') as f:
    config = yaml.load(f)

LIST = config["the-list"]
ojdo
  • 8,280
  • 5
  • 37
  • 60
user3264073
  • 21
  • 2
  • 4

1 Answers1

2

I had given a clue to the list datatype in my comment. Simply append the new item to the list:

config["the-list"].append("newstring")
with file('config.yml', 'w') as f:
    yaml.dump(config, f) 
ojdo
  • 8,280
  • 5
  • 37
  • 60