python的yaml.load与yaml.dump输出不一致问题

使用yaml.dump写saltstack的sls文件发现跟yaml.load出来的再dump回去格式不一样

#!/usr/bin/python3
# -*-coding:utf-8-*-
data = yaml.load("""
    base:
      game_1.1.1.1: 
        - games.game1111
      game_2.2.2.2: 
        - games.game2222
""")

print(data)
print('#' * 50)
print(yaml.dump(data))

执行后输出,发现dump出来的格式竟然和data里传入的格式不一样:

{'base': {'game_1.1.1.1': ['games.game1111'], 'game_2.2.2.2': ['games.game2222']}}
##################################################
base:
  game_1.1.1.1: [games.game1111]
  game_2.2.2.2: [games.game2222]

一番查找,最后发现了dump中有个 default_flow_style=False 参数

print(yaml.dump(data, default_flow_style=False))

执行输出:

{'base': {'game_1.1.1.1': ['games.game1111'], 'game_2.2.2.2': ['games.game2222']}}
##################################################
base:
  game_1.1.1.1:
  - games.game1111
  game_2.2.2.2:
  - games.game2222

官方文档:https://pyyaml.org/wiki/PyYAMLDocumentation

你可能感兴趣的:(python的yaml.load与yaml.dump输出不一致问题)