简体   繁体   English

Python YAML保留换行符而不添加额外的换行符

[英]Python YAML preserving newline without adding extra newline

I have a similar problem to this question that I need to insert newlines in a YAML mapping value string and prefer not to insert \\n myself. 我对这个问题有一个类似的问题,我需要在YAML映射值字符串中插入换行符,而不想自己插入\\n The answer suggests using: 答案建议使用:

Data: |
      Some data, here and a special character like ':'
      Another line of data on a separate line

instead of 代替

Data: "Some data, here and a special character like ':'\n
      Another line of data on a separate line"

which also adds a newline at the end , that is unacceptable. 这也在末尾添加一个新行,即不能接受的。

I tried using Data: > but that showed to give completely different results. 我尝试使用Data: >但是这显示出完全不同的结果。 I have been stripping the final newline after reading in the yaml file, and of course that works but that is not elegant. 我在读完yaml文件后一直在剥离最终换行符,当然这样可行,但这并不优雅。 Any better way to preserve newlines without adding an extra one at the end ? 没有更好的方法来保留换行符而不在末尾添加额外的换行符?

I am using python 2.7 fwiw 我正在使用python 2.7 fwiw

If you use | 如果你使用| This makes a scalar into a literal block style scalar . 这使得标量成为文字块样式标量 But the default behaviour of | 但是|的默认行为 , is clipping and that doesn't get you the string you wanted (as that leaves the final newline). ,是剪辑,并没有得到你想要的字符串(因为它留下了最后的换行符)。

You can "modify" the behaviour of | 您可以“修改” |的行为 by attaching block chomping indicators 通过附加块扼流指示器

Strip 跳闸

Stripping is specified by the “-” chomping indicator. 剥离由“ - ”扼流指示器指定。 In this case, the final line break and any trailing empty lines are excluded from the scalar's content. 在这种情况下,最终换行符和任何尾随空行都会从标量内容中排除。

Clip

Clipping is the default behavior used if no explicit chomping indicator is specified. 如果未指定显式的chomping指示符,则剪切是使用的默认行为。 In this case, the final line break character is preserved in the scalar's content. 在这种情况下,最终换行符将保留在标量的内容中。 However, any trailing empty lines are excluded from the scalar's content. 但是,任何尾随空行都会从标量内容中排除。

Keep 保持

Keeping is specified by the “+” chomping indicator. 保持由“+”扼流指示器指定。 In this case, the final line break and any trailing empty lines are considered to be part of the scalar's content. 在这种情况下,最后的换行符和任何尾随的空行被认为是标量内容的一部分。 These additional lines are not subject to folding. 这些额外的线条不会折叠。

By adding the stripchomping operator ' - ' to ' | 通过将stripchomping运算符' - '添加到' | ', you can prevent/strip the final newline:¹ ',你可以阻止/剥离最后的换行符:¹

import ruamel.yaml as yaml

yaml_str = """\
Data: |-
      Some data, here and a special character like ':'
      Another line of data on a separate line
"""

data = yaml.load(yaml_str)
print(data)

gives: 得到:

{'Data': "Some data, here and a special character like ':'\\nAnother line of data on a separate line"} {'数据':“某些数据,此处和特殊字符如':'\\ n另一行上的另一行数据”}


¹ This was done using ruamel.yaml of which I am the author. ¹ 这是使用ruamel.yaml完成的,我是作者。 You should get the same result with PyYAML (of which ruamel.yaml is a superset, preserving comments and literal scalar blocks on round-trip). 你应该得到与PyYAML相同的结果(其中ruamel.yaml是一个超集,保留了往返的注释和文字标量块)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM