我正在生成用作协议的 YAML,其中包含一些生成的 JSON。
import json
from ruamel import yaml
jsonsample = { "id": "123", "type": "customer-account", "other": "..." }
myyamel = {}
myyamel['sample'] = {}
myyamel['sample']['description'] = "This example shows the structure of the message"
myyamel['sample']['content'] = json.dumps( jsonsample, indent=4, separators=(',', ': '))
print yaml.round_trip_dump(myyamel, default_style = None, default_flow_style=False, indent=2, block_seq_indent=2, line_break=0, explicit_start=True, version=(1,1))
然后我得到这个输出
%YAML 1.1
---
sample:
content: "{\n \"other\": \"...\",\n \"type\": \"customer-account\",\n \"\
id\": \"123\"\n}"
description: This example shows the structure of the message
现在对我来说,如果我能够从管道开始格式化多行行,那看起来会更好|
我想看到的输出是这个
%YAML 1.1
---
sample:
content: |
{
"other": "...",
"type": "customer-account",
"id": "123"
}
description: This example shows the structure of the message
看看这是多么容易阅读......
那么如何在 python 代码中解决这个问题呢?