使用 ruamel.yaml,如何使带有 NEWLINE 的 var 成为不带引号的多行
Using ruamel.yaml, how can I make vars with NEWLINEs be multiline without quotes
我正在生成用作协议的 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 代码中解决这个问题?
你可以这样做:
import sys
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=(',', ': '))
yaml.scalarstring.walk_tree(myyamel)
yaml.round_trip_dump(myyamel, sys.stdout, 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:
description: This example shows the structure of the message
content: |-
{
"id": "123",
"type": "customer-account",
"other": "..."
}
一些注意事项:
由于您使用的是普通字典,因此打印 YAML 的顺序取决于实现和密钥。如果您希望订单固定在您的作业中,请使用:
myyamel['sample'] = yaml.comments.CommentedMap()
- 如果打印 return 值,则永远不要使用
print(yaml.round_trip_dump)
,指定要写入的流,这样效率更高。
walk_tree
将所有包含换行符的字符串递归地转换为块样式模式。你也可以明确地做:
myyamel['sample']['content'] = yaml.scalarstring.PreservedScalarString(json.dumps( jsonsample, indent=4, separators=(',', ': ')))
在这种情况下你不需要调用 walk_tree()
即使您仍在使用 Python 2,您也应该开始习惯使用 print
函数而不是 print
语句。为此,请在每个 Python 文件的顶部包含:
from __future__ import print_function
我正在生成用作协议的 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 代码中解决这个问题?
你可以这样做:
import sys
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=(',', ': '))
yaml.scalarstring.walk_tree(myyamel)
yaml.round_trip_dump(myyamel, sys.stdout, 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:
description: This example shows the structure of the message
content: |-
{
"id": "123",
"type": "customer-account",
"other": "..."
}
一些注意事项:
由于您使用的是普通字典,因此打印 YAML 的顺序取决于实现和密钥。如果您希望订单固定在您的作业中,请使用:
myyamel['sample'] = yaml.comments.CommentedMap()
- 如果打印 return 值,则永远不要使用
print(yaml.round_trip_dump)
,指定要写入的流,这样效率更高。 walk_tree
将所有包含换行符的字符串递归地转换为块样式模式。你也可以明确地做:myyamel['sample']['content'] = yaml.scalarstring.PreservedScalarString(json.dumps( jsonsample, indent=4, separators=(',', ': ')))
在这种情况下你不需要调用
walk_tree()
即使您仍在使用 Python 2,您也应该开始习惯使用 print
函数而不是 print
语句。为此,请在每个 Python 文件的顶部包含:
from __future__ import print_function