Python pyyaml 的漂亮输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24418449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pretty output with pyyaml
提问by Mayur Patel
I have a python project where I'd like to use YAML (pyYaml 3.11), particularly because it is "pretty" and easy for users to edit in a text editor if and when necessary. My problem, though, is if I bring the YAML into a python application (as I will need to) and edit the contents (as I will need to) then writing the new document is typically not quite as pretty as what I started with.
我有一个 python 项目,我想在其中使用 YAML (pyYaml 3.11),特别是因为它“漂亮”并且用户在必要时可以轻松地在文本编辑器中进行编辑。不过,我的问题是,如果我将 YAML 带入 Python 应用程序(因为我需要)并编辑内容(因为我需要),那么编写新文档通常不像我开始时那么漂亮。
The pyyaml documentation is pretty poor - does not even document the parameters to the dump function. I found http://dpinte.wordpress.com/2008/10/31/pyaml-dump-option/. However, I'm still missing the information I need. (I started to look at the source, but it doesn't seem the most inviting. If I don't get the solution here, then that's my only recourse.)
pyyaml 文档很差 - 甚至没有记录转储函数的参数。我找到了http://dpinte.wordpress.com/2008/10/31/pyaml-dump-option/。但是,我仍然缺少我需要的信息。(我开始查看源,但它似乎不是最吸引人的。如果我在这里没有得到解决方案,那么这是我唯一的办法。)
I start with a document that looks like this:
我从一个看起来像这样的文档开始:
- color green : inputs : - port thing : widget-hint : filename widget-help : Select a filename - port target_path : widget-hint : path value : 'thing' outputs: - port value: widget-hint : string text : | I'm lost and I'm found and I'm hungry like the wolf.
After loading into python (yaml.safe_load( s )), I try a couple ways of dumping it out:
加载到 python (yaml.safe_load( s )) 后,我尝试了几种转储方法:
>>> print yaml.dump( d3, default_flow_style=False, default_style='' ) - color green: inputs: - port thing: widget-help: Select a filename widget-hint: filename - port target_path: value: thing widget-hint: path outputs: - port value: widget-hint: string text: 'I''m lost and I''m found and I''m hungry like the wolf. '
>>> print yaml.dump( d3, default_flow_style=False, default_style='|' ) - "color green": "inputs": - "port thing": "widget-help": |- Select a filename "widget-hint": |- filename - "port target_path": "value": |- thing "widget-hint": |- path "outputs": - "port value": "widget-hint": |- string "text": | I'm lost and I'm found and I'm hungry like the wolf.
Ideally, I would like "short strings" to not use quotes, as in the first result. But I would like multi-line strings to be written as blocks, as with the second result. I guess fundamentally, I'm trying to minimize an explosion of unnecessary quotes in the file which I perceive would make it much more annoying to edit in a text editor.
理想情况下,我希望“短字符串”不使用引号,就像第一个结果一样。但是我希望将多行字符串写为块,就像第二个结果一样。我想从根本上说,我试图尽量减少文件中不必要的引号爆炸,我认为这会使在文本编辑器中编辑变得更加烦人。
Does anyone have any experience with this?
有人对这个有经验么?
采纳答案by Anthon
If you can use ruamel.yaml(disclaimer: I am the author of this enhanced version of PyYAML) you can do:
如果您可以使用ruamel.yaml(免责声明:我是 PyYAML 增强版的作者),您可以执行以下操作:
import ruamel.yaml
yaml_str = """\
- color green :
inputs :
- port thing :
widget-hint : filename
widget-help : Select a filename
- port target_path :
widget-hint : path
value : 'thing'
outputs:
- port value:
widget-hint : string
text : |
I'm lost and I'm found
and I'm hungry like the wolf.
"""
data = ruamel.yaml.round_trip_load(yaml_str)
res = ""
for line in ruamel.yaml.round_trip_dump(data, indent=5, block_seq_indent=3).splitlines(True):
res += line[3:]
print(res)
you get:
你得到:
- color green:
inputs:
- port thing:
widget-hint: filename
widget-help: Select a filename
- port target_path:
widget-hint: path
value: thing
outputs:
- port value:
widget-hint: string
text: |
I'm lost and I'm found
and I'm hungry like the wolf.
Not exactly what you did start out with (but after this round-trip it is stable). With more recent versions of ruamel.yaml you can set both the indent and the relative indent of a sequence -
within that indent. The latter however also influences your top-level sequence, hence the post processing.
不完全是你开始时所做的(但在这次往返之后它是稳定的)。使用 ruamel.yaml 的更新版本,您可以在该缩进中设置序列的缩进和相对-
缩进。然而,后者也会影响您的顶级序列,从而影响后期处理。
Important (to me) things that are preserved: comments, anchors, mapping merges, and literal scalars ( multi-line using |
)
保留的重要(对我而言)事物:注释、锚点、映射合并和文字标量(多行使用|
)
回答by Aryeh Leib Taurog
Try the pyamlpretty printer. It gets closer, though it does put quotes around short strings with spaces in them:
试试pyaml漂亮的打印机。它变得更近了,尽管它确实在短字符串周围加上了引号,其中包含空格:
>>> print pyaml.dump(d3)
- 'color green':
inputs:
- 'port thing':
widget-help: 'Select a filename'
widget-hint: filename
- 'port target_path':
value: thing
widget-hint: path
outputs:
- 'port value':
widget-hint: string
text: |
I'm lost and I'm found
and I'm hungry like the wolf.