Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

YAML library giving two different outputs

The expected output format is as follows:

- start: [45, 78]
- goal: [56, 89]

I tried the following code on two different machines:

import yaml 

dict_file = [{'start' : [45,78]},
         {'goal' : [56, 89]}]

with open('store_file.yaml', 'w') as file:
   documents = yaml.dump(dict_file, file)

Machine with YAML 3.12 gives:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

- start: [45, 78]
- goal: [56, 89]

Machine with YAML 6.0 gives:

  - start:
    - 45
    - 78
  - goal:
    - 56
    - 89

However, I want to get the same output (as in the machine which has YAML 3.12) in the machine which has YAML 6.0

>Solution :

You can achieve the abbreviated form for the list by setting default_flow_style argument to None.

import yaml

dict_file = [{'start': [45, 78]},
             {'goal': [56, 89]}]

with open('store_file.yaml', 'w') as file:
    documents = yaml.dump(dict_file, file, default_flow_style=None)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading