YAML 库提供两种不同的输出

YAML library giving two different outputs

预期的输出格式如下:

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

我在两台不同的机器上尝试了以下代码:

import yaml 

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

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

带有 YAML 3.12 的机器给出:

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

带有 YAML 6.0 的机器给出:

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

但是,我想在装有 YAML 6.0 的机器上获得相同的输出(与装有 YAML 3.12 的机器一样)

您可以通过将 default_flow_style 参数设置为 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)