프로토콜로 작동하는 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
이것이 얼마나 쉽게 읽을 수 있는지보십시오 ...
그래서 어떻게 이것을 파이썬 코드로 해결할 수 있습니까?
YAML 파일이 완전히 동일하지 않습니다. '|'를 사용해야하기 때문에 (''strip * block chomping indicator ''와 함께 블록 스타일을 사용하라.) (http :// /yaml.org/spec/1.2/spec.html#id2794534) – Anthon