YAML 多行数组 [英] YAML Multi-Line Arrays

查看:44
本文介绍了YAML 多行数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

YAML中,您可以轻松创建多行字符串.但是,我希望能够使用 | 字符创建多行数组(主要是为了配置文件中的可读性).

In YAML, you can easily create multi-line strings. However, I would like the ability to create a multi-line array (mainly for readibility within config files) using the | character.

一个 YAML 数组 可以表示为:['key1', 'key2', 'key3'].

A YAML array can be represented as: ['key1', 'key2', 'key3'].

一个 YAML 序列 使用一个破折号,后跟一个空格,然后是一个字符串:

A YAML sequence uses a dash followed by a space and then a string:

- String1
- String2
- String3

这将计算为:['string1', 'string2', 'string3'].

YAML 映射 是我们在 YAML 中一直看到的键值对数组:

A YAML mapping is an array of key and value pairs that we see all the time in YAML:

Key1: string1
Key2: string2
Key3: string3

这一切都很好,但我一辈子都看不到如何做一个多线阵列.像这样:

This is all well and good, but I can't for the life of me see how to do a multi-line array. Something like this:

|
['string1', 'string2', 'string3']
['string4', 'string5', 'string6']

除了在 YAML 中创建多个数组映射并在我选择的编程语言中合并它们之外,有没有什么方法可以实现多行数组,也许使用 { } 像 Python 那样但在 YAML 中?

Short of creating multiple array mappings in YAML and merging them in my programming language of choice, is there any way to achieve multi-line arrays, maybe with { } like Python has but in YAML?

推荐答案

一个 YAML 序列 是一个 数组.所以这是正确的表达方式:

A YAML sequence is an array. So this is the right way to express it:

key:
  - string1
  - string2      
  - string3
  - string4
  - string5
  - string6

意思相同:

key: ['string1', 'string2', 'string3', 'string4', 'string5', 'string6']

将单行数组拆分成多行也是合法的:

It's also legal to split a single-line array over several lines:

key: ['string1', 'string2', 'string3', 
  'string4', 'string5', 
  'string6']

甚至在单行数组中有多行字符串:

and even have multi-line strings in single-line arrays:

key: ['string1', 'long
  string', 'string3', 'string4', 'string5', 'string6']

这篇关于YAML 多行数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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