写入具有特定格式的yaml文件

时间:2017-04-11 11:00:37

标签: python yaml

我有以下yaml

---  
- - :whitelist
  - MIT
  - :who: 
    :why: 
    :when: 2015-10-14 11:59:36.990687307 Z

使用

读取此yaml文件
import yaml
items = None
with open('yaml_file.yml') as yaml_file:
    items = yaml.load(yaml_file):

给我一​​个这样的项目列表:

[':whitelist', 'MIT', {':who': None, ':why': None, ':when': datetime.datetime(2015, 10, 14, 11, 59, 36, 990687)}]

但是试图做对话:

data = [':whitelist', 'MIT', {':who': None, ':why': None, ':when': datetime.datetime(2015, 10, 14, 11, 59, 36, 990687)}]
with open('yaml_file.yml', 'w') as yaml_file:
    yaml.dump(data, yaml_file, default_flow_style=False)

给我这种格式

- :whitelist
- MIT
- :when: 2015-10-14 11:59:36.990687
  :who: null
  :why: null

如何在yaml文件上实现相同的格式,并在转储字典时将None表示为空字符串?

0 个答案:

没有答案