By Harsha Biyani


2018-10-11 14:22:54 8 Comments

I have a dictionary, I am converting dictionary to yaml using yaml module in python. But Yaml is not converting properly.

output_data = {
    'resources': [{
        'type': 'compute.v1.instance',
        'name': 'vm-created-by-deployment-manager',
        'properties': {
            'disks': [{
                'deviceName': '$disks_deviceName$',
                'boot': '$disks_boot$',
                'initializeParams': {
                    'sourceImage': '$disks_initializeParams_sourceImage$'
                },
                'autoDelete': '$disks_autoDelete$',
                'type': '$disks_type$'
            }],
            'machineType': '$machineType$',
            'zone': '$zone$',
            'networkInterfaces': [{
                'network': '$networkInterfaces_network$'
            }]
        }
    }]
}

I tried :

import yaml
f = open('meta.yaml', 'w+')
yaml.dump(output_data, f, allow_unicode=True)

I am getting meta.yaml file as following:

resources:
- name: vm-created-by-deployment-manager
  properties:
    disks:
    - autoDelete: $disks_autoDelete$
      boot: $disks_boot$
      deviceName: $disks_deviceName$
      initializeParams: {sourceImage: $disks_initializeParams_sourceImage$}
      type: $disks_type$
    machineType: $machineType$
    networkInterfaces:
    - {network: $networkInterfaces_network$}
    zone: $zone$
  type: compute.v1.instance

Here, {sourceImage: $disks_initializeParams_sourceImage$} and {network: $networkInterfaces_network$} is getting like dictionary. It means inner dictionary contents are not converting to yaml.

I also tried,

output_data = eval(json.dumps(output_data)) 
ff = open('meta.yaml', 'w+')
yaml.dump(output_data, ff, allow_unicode=True)

But getting same yaml file content.

How can I convert complete dictinary to yaml in Python?

1 comments

@floydya 2018-10-11 14:35:40

By default, PyYAML chooses the style of a collection depending on whether it has nested collections. If a collection has nested collections, it will be assigned the block style. Otherwise it will have the flow style.

If you want collections to be always serialized in the block style, set the parameter default_flow_style of dump() to False. For instance,

> `print(yaml.dump(yaml.load(document), default_flow_style=False))`
>> Result: `a: 1 b:   c: 3   d: 4`

Documentation: https://pyyaml.org/wiki/PyYAMLDocumentation

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] What is the best way to iterate over a dictionary?

  • 2008-09-26 18:20:06
  • Jake Stewart
  • 1375097 View
  • 2280 Score
  • 27 Answer
  • Tags:   c# dictionary loops

10 Answered Questions

[SOLVED] How to remove a key from a Python dictionary?

40 Answered Questions

[SOLVED] How to merge two dictionaries in a single expression?

13 Answered Questions

[SOLVED] Add new keys to a dictionary?

16 Answered Questions

[SOLVED] Check if a given key already exists in a dictionary

  • 2009-10-21 19:05:09
  • Mohan Gulati
  • 2908415 View
  • 2683 Score
  • 16 Answer
  • Tags:   python dictionary

10 Answered Questions

[SOLVED] Iterating over dictionaries using 'for' loops

21 Answered Questions

34 Answered Questions

[SOLVED] How do I sort a dictionary by value?

15 Answered Questions

[SOLVED] What are metaclasses in Python?

56 Answered Questions

[SOLVED] Calling an external command in Python

Sponsored Content