title: How do I edit a YAML file through python using ruamel.yaml and not pyyaml?
body: <p>I found a similar questions here on StackOverflow, but when I tried to implement the code [<a href="https://stackoverflow.com/questions/29518833/editing-yaml-file-by-python">Anthons's response</a>] to suit my situation, I noticed that it does not actually edit the YAML file. Also, I need to use ruamel.yaml, not PyYAML. Many of the answers I've reviewed use PyYAML. </p>
  
  <pre><code>import sys
  import ruamel.yaml  
  
  yaml = ruamel.yaml.YAML()
  # yaml.preserve_quotes = True
  
  with open('elastic.yml') as fp:
      data = yaml.load(fp)
  
  data['cluster.name'] = 'BLABLABLABLABLA'
  data['node.name'] = 'HEHEHEHEHEHEHE'
  
  yaml.dump(data, sys.stdout)
  </code></pre>
  
  <p>This code outputs the file with the correct edits, however, when I actually go into the file (<code>elastic.yml</code>), the original documentation is unchanged. </p>
  
  <p>This is my first experience with ruamel.yaml and I would rather stick with this because I've noticed PyYAML does not keep comments.</p>
  
  <p>The YAML file after I run the python code:</p>
  
  <pre><code>cluster.name: my-application
  
  # Use a descriptive name for the node:
  node.name: HappyNode    
  </code></pre>
  
  <p>The output to the console after I run the python code:</p>
  
  <pre><code>cluster.name: BLABLABLABLABLA
  
  # Use a descriptive name for the node:
  node.name: HEHEHEHEHEHEHE
  </code></pre>
  
  <p>I've tried adding this to the bottom of the code to assure it write to the file, as described here:[<a href="https://stackoverflow.com/questions/40762382/changing-a-value-in-a-yaml-file-using-python">Matheus Portela's response</a>] but I get no luck: </p>
  
  <pre><code>with open('elastic.yml', 'w') as f:
          yaml.dump(data, f)
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>data['cluster.name'] = 'BLABLABLABLABLA'
  TypeError: 'NoneType' object does not support item assignment
  </code></pre>