私はプロトコルとして機能するYAMLを生成しており、生成されたJSONをいくつか含んでいます。ruamel.yamlを使用して、NEWLINEを複数行にして引用符なしにすることはできますか?
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コードで解決できますか?
あなたのYAMLファイルは完全に同じではありません。 '|'を使うべきであるということを取り除くために、 '| * 'を使用するので、最後の'} 'の後ろに改行が1つあります(つまり、[* strip * block chomping indicator] /yaml.org/spec/1.2/spec.html#id2794534) – Anthon