Preserve new lines in YAML

末鹿安然 提交于 2019-12-09 02:12:17

问题


How do I format a YAML document like this so that PyYAML can parse it properly?

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

I know that the ':' character is special so I have to surround the whole thing in quotations like so:

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

And in order to add a new line, I have to add '\n':

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

Is there anyway to format the YAML document so I don't have to add the '\n's in order to have a new line?


回答1:


For multi-line scalars, you can use blocks. The character | denotes the start of a block. Use:

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



回答2:


If the extra newline that NullUserException's solutions is adding is a problem you should be using:

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


来源:https://stackoverflow.com/questions/3748585/preserve-new-lines-in-yaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!