title: YAML - Appending Content before comments/New lines
body: <p>I have the following YAML file</p>
  <p><strong>template.yaml</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>AWSTemplateFormatVersion: '2010-09-09'
  Transform: AWS::Serverless-2016-10-31
  Description: &gt;
      sam-app
  
      Sample SAM Template for sam-app
  
  # More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
  Globals:
      Function:
          Timeout: 20
          MemorySize: 512
          Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
            Variables:
              TABLE_NAME: !Ref OrdersTable
              ENDPOINT_OVERRIDE: &quot;&quot;
  
  Resources:
    GetOrderFunction:
      Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
      Properties:
        CodeUri: target/aws-sam-java-rest-1.0.0.jar
        Handler: com.amazonaws.handler.GetOrderHandler::handleRequest
        Runtime: java8
        Policies:
          - DynamoDBReadPolicy:
              TableName: !Ref OrdersTable
        Events:
          GetOrder:
            Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
            Properties:
              Path: /orders/{order_id}
              Method: get
    OrdersTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: orders_table
        AttributeDefinitions:
          - AttributeName: orderId
            AttributeType: S
        KeySchema:
          - AttributeName: orderId
            KeyType: HASH
        BillingMode: PAY_PER_REQUEST
  
  
  Outputs:
      HelloWorldApi:
        Description: &quot;API Gateway endpoint URL for Prod stage for Hello World function&quot;
        Value: !Sub &quot;https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/&quot;
  
      GetOrderFunction:
        Description: &quot;GetOrder Lambda Function ARN&quot;
        Value: !GetAtt GetOrderFunction.Arn
  
      GetOrdersFunction:
        Description: &quot;GetOrders Lambda Function ARN&quot;
        Value: !GetAtt GetOrdersFunction.Arn
  
      UpdateOrderFunction:
        Description: &quot;UpdateOrder Lambda Function ARN&quot;
        Value: !GetAtt UpdateOrderFunction.Arn
  
      DeleteOrderFunction:
        Description: &quot;DeleteOrder Lambda Function ARN&quot;
        Value: !GetAtt DeleteOrderFunction.Arn
  
      CreateOrderFunction:
        Description: &quot;CreateOrder Lambda Function ARN&quot;
        Value: !GetAtt CreateOrderFunction.Arn
  </code></pre>
  <p>and I am trying to add another Property to <code>Resources:OrdersTable:Properties</code> the following way</p>
  <p><strong>main.py</strong></p>
  <pre class="lang-py prettyprint-override"><code>from ruamel.yaml import YAML
  yaml = YAML()
  with open('template.yaml', 'r') as f:
      dataMap = yaml.load(f)
      dataMap['Resources']['OrdersTable']['Properties']['Tags'] = [
          {
              'Key': 'Name1',
              'Value': 'Value1'
          },
          {
              'Key': 'Name2',
              'Value': 'Value2'
          },
          {
              'Key': 'Name3',
              'Value': 'Value3'
          }]
          yaml.dump(dataMap, open('new.yaml', 'w'))
  </code></pre>
  <p>the problem is that the output file keeps the new lines that was between <code>BillingMode</code> and <code>Outputs</code></p>
  <p>here is the output file (only the effected section)</p>
  <p><strong>new.yaml</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>Resources:
    OrdersTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: orders_table
        AttributeDefinitions:
        - AttributeName: orderId
          AttributeType: S
        KeySchema:
        - AttributeName: orderId
          KeyType: HASH
        BillingMode: PAY_PER_REQUEST
  
  
        Tags:
        - Key: Name1
          Value: Value1
        - Key: Name2
          Value: Value2
        - Key: Name3
          Value: Value3
  Outputs:
    HelloWorldApi:
      Description: API Gateway endpoint URL for Prod stage for Hello World function
      Value: !Sub &quot;https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/&quot;
  </code></pre>
  <p>and this is how I want the new file to be</p>
  <pre class="lang-yaml prettyprint-override"><code>Resources:
    OrdersTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: orders_table
        AttributeDefinitions:
        - AttributeName: orderId
          AttributeType: S
        KeySchema:
        - AttributeName: orderId
          KeyType: HASH
        BillingMode: PAY_PER_REQUEST
        Tags:
        - Key: Name1
          Value: Value1
        - Key: Name2
          Value: Value2
        - Key: Name3
          Value: Value3
  
  
  Outputs:
    HelloWorldApi:
      Description: API Gateway endpoint URL for Prod stage for Hello World function
      Value: !Sub &quot;https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/&quot;
  </code></pre>
  <p>I've found that the BillingMode map looks like this
  <code>{'BillingMode': [None, None, CommentToken('\n\n\n', line: 104, col: 19), None]}</code>, but I dont know what to do with that knowledge.
  Any help will be appreciated.</p>