Home > database >  YAML library giving two different outputs
YAML library giving two different outputs

Time:04-07

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:

- 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

CodePudding user response:

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)
  • Related