Jul 6, 2017 - ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order. From ruamel.yaml import YAML #To create YAML File and Read YAML File. Inpfo = open('data.yml').read ## Read the Yaml File.
This is my YAML file (
input.yaml
):I want to edit this YAML file to add more values in key 'hobbies', example:
I tried to implement the code Anthon to fit my situation but it didn't helped at all, because the indention level of that YAML file is different from mine.
Example:
Example:
I get error 'TypeError('string indices must be integers',)', I know this code might be completely wrong, but I am new to ruamel.yaml.
How to code this?
Anthon35k1717 gold badges101101 silver badges159159 bronze badges
MEHUL SOLANKIMEHUL SOLANKI
2 Answers
The thing missing form the error message displayed is the line number (I assume that it is 9). That points to the line
And if that doesn't give you a clue, the approach that I recommend in such cases is starting to add some
print
functions, so that you know what you are working on. The for
loop looks like:this prints
before the exception is thrown, and I hope that will make you realize your are not iterating over the elements (items) of a list, but over the keys of a dict (constructed from the root level mapping in your YAML). And the value associated with the key is the object having a key
name
and a key hobbies
.So change the variable
elem
to key
to make clear what you're handling and then proceed to work with value
, the value associated with that key instead of elem
within that loop¹:This gives:
So we got rid of the exception, but the result is not exactly what you want. The element
dancing
for the key 'hobbies' is gone, because you assign a new (list) value to that key, whereas what you should do is append a single item to the list. We can also get rid of the print function by now:This will get you two items in the final sequence in the file. There is a few more things to address:
- the capitalization of
dancing
incorrect. To correct that, add a line handling the list if there is only one element - the code for the name
Max
, needs to be added (and that is why you need to get rid of thebreak
in your code) - the empty line, is considered a comment on the last element of the first sequence, that comment needs to be moved
- your indentation of sequences is non-default
The final code would be like:
Which gives something quite close to what you wanted to get
¹Alternative for the first two lines:
AnthonAnthonfor key, value in data.items()
35k1717 gold badges101101 silver badges159159 bronze badges
Thanks Anthon your code worked I have to edit this code as follows:
MEHUL SOLANKIMEHUL SOLANKI
Not the answer you're looking for? Browse other questions tagged pythonyamlruamel.yaml or ask your own question.
I have a YAML file that looks like this:
I want to use Python to open this file, change some of the values (see above) and close the file. How can I do that ?
For instance I want to set sense2[value]=1234, keeping the YAML output the same.
Anthon35k1717 gold badges101101 silver badges159159 bronze badges
Tzimkiyahoo Bar KozyvaTzimkiyahoo Bar Kozyva
2 Answers
Anthon35k1717 gold badges101101 silver badges159159 bronze badges
jwilnerjwilner3,68233 gold badges2222 silver badges3838 bronze badges
If you care about preserving the order of your mapping keys, the comment and the white space between the elements of the root-level sequence, e.g. because this file is under revision control, then you should use
ruamel.yaml
(disclaimer: I am the author of that package). Assuming your YAML document is in the file
input.yaml
:gives:
AnthonAnthon
35k1717 gold badges101101 silver badges159159 bronze badges