YAML 中管道符号的用途是什么? [英] What is the use of the pipe symbol in YAML?

查看:19
本文介绍了YAML 中管道符号的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 yaml 新手,我对用于多行的管道符号 (|) 有疑问.YAML 是否有类似下面的语法?

I am new to yaml, and I have a question about the pipe symbol (|) used for multiple lines. Does YAML have any syntax like the one below?

测试:|6+

test: |6+

在下面的两个 YAML 文件中,第一个有效,第二个无效.我不知道是什么原因造成的.

Of the two YAML files below, the first one is working and second is not. I do not know what is causing this.

第一个文件

Name :
  -
   testing:
     val1
  -
   second:
     val2
  -
   third:
     val3
  -
   then
  - 
    final: |
     a
     aa
     aaa
     aaaa : 'test:'

第二个文件

Name :
  -
   testing:
     val1
  -
   second:
     val2
  -
   third:
     val3
  -
   then
  - 
    final: |6+
      a
      aa
      aaa
      aaaa : 'test:'

第二个文件是客户的.

我正在使用 XMLBeans,但出现此错误:

I am using XMLBeans and I get this error:

com.esotericsoftware.yamlbeans.parser.Parser$ParserException:第 17 行,第 12 列:应为块结束",但发现:块映射开始.

推荐答案

YAML 中行尾的管道符号表示后面的任何缩进文本都应解释为多行标量值.请参阅 YAML 规范.

The pipe symbol at the end of a line in YAML signifies that any indented text that follows should be interpreted as a multi-line scalar value. See the YAML spec.

具体来说,竖线表示(除了缩进)标量值应该以保留换行符的方式逐字解释.相反,> 字符表示多行折叠";标量紧随其后,这意味着换行符被转换为空格.例如:

Specifically, the pipe indicates that (except for the indentation) the scalar value should be interpreted literally in such a way that preserves newlines. Conversely, the > character indicates that multi-line "folded" scalar follows, meaning that newlines are converted to spaces. For example:

>>> import yaml
>>> yaml.load("""
... |
...  This is a multi-line
...  literal style scalar.
... """)
'This is a multi-line
literal style scalar.
'
>>> yaml.load("""
... >
...  This is a multi-line
...  folded scalar; new lines are folded into
...  spaces.
... """)
'This is a multi-line folded scalar; new lines are folded into spaces.
'

6+ 部分是缩进指示符(明确说明应该使用多少个缩进空格)和chomping 指示符".+ 控制如何处理标量文字末尾的额外空格.

The 6+ part is the indentation indicator (an explicit specification of how many spaces of indentation should be used) with the "chomping indicator" + which controls how extra whitespace at the end of the scalar literal should be handled.

您得到的错误是一个棘手的错误:这是因为缩进应该与当前的块级元素相关.所以在这种情况下,它应该是 2+ 而不是 6+ 因为最后一个块级元素是映射 final: 并且文字是从中缩进 2.更新了来自@bramvi 的更正.

The error you're getting is a tricky one: It's because indentation should be relative to the current block-level element. So in this case it should be 2+ instead of 6+ because the last block-level element is the mapping final: and the literal is indented 2 from it. Updated with correction from @bramvi.

这篇关于YAML 中管道符号的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆