当前位置:网站首页>「译文」如何在YAML中输入多行字符串?
「译文」如何在YAML中输入多行字符串?
2022-04-22 15:11:00 【东风微鸣】
️URL: https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines Description: In YAML, I have a string that's very long. I want to keep this within the 80-column (or so) view of my editor, so I'd like to break the string. What's the syntax for this?
问题
在YAML中,我有一个非常长的字符串。我希望将其保存在编辑器的80列(大约)视图中,因此我想中断字符串。它的语法是什么? 换句话说,我有:
Key: 'this is my very very very very very very long string'
我想要这样(或类似的效果):
Key: 'this is my very very very ' +
'long string'
我想像上面那样使用引号,所以我不需要转义字符串中的任何内容
答案
在 YAML 中有很多不同的方法来编写多行字符串。
TL;DR
符号 >
大多数情况下使用: > 去掉内部换行符,但是在末尾会有一个换行符:
key: >
Your long
string here.
符号 |
如果您希望这些换行符保持为\n(举例,就是嵌入段落的标记),请使用|。
key: |
### Heading
* Bullet
* Points
>- 或 |-
如果不希望在末尾追加换行符,则使用>- 或 |-.
符号 ""
如果你需要在单词中间分隔行,或者想按字面意思键入换行符\n:
key: "Antidisestab\
lishmentarianism.\n\nGet on it."
YAML 太诡异了🤬🤬🤬
块标量样式(>,|)
它们允许像\和"这样的字符不需要转义,并在字符串的末尾添加一个新行(\n)。
> 折叠样式[1]删除了字符串中的单个换行符(但在结尾处增加一个,并将双换行符转换为单换行符)。
Key: >
this is my very very very
long string
结果是: this is my very very very long string\n
额外的前导空间被保留并导致额外的换行符。请参阅下面的注意。
️ 建议: 字符串 wrap line 使用这个。通常这就是你想要的。
| 字面风格[2]将字符串中的每一个换行(newline)都变成一个字面换行(literal newline),并在最后加上一个换行。
Key: |
this is my very very very
long string
结果是: this is my very very very\nlong string\n
下面是YAML规范1.2[3]的官方定义:
️ Reference: 标量内容可以用块状符号来写,使用字面风格(用
|表示),其中所有的换行符都是显式的。另外,也可以使用折叠式风格(用>表示),其中每个换行符都被折叠成一个空格,除非它结束了一个空行或一个缩进较大的行。️ 建议: 将格式化的文本(特别是Markdown)作为值插入使用这个
|。
带有块咬合指示器(block chomping indicator)的块状样式(>-, |-, >+, |+)
你可以通过添加一个块咬合指示符[4]字符来控制字符串中最后一个新行和任何尾随空行(\n\n)的处理:
•>, |: “剪辑(clip)”:保留换行,删除末尾空行。
•>-, |-: "strip":删除换行,删除末尾的空行。
•>+, |+: "keep":保持换行,末尾为空行。
“流(Flow)”标量风格 (, ", ')
它们有有限的转义,并构造一个没有新行字符的单行字符串。它们可以在与键相同的行开始,或者在前面附加换行符,这些换行符被剥离。双行换行符变成一个换行符。
plain style[5](没有转义,没有#或:组合,第一个字符不能是",'或许多其他标点字符):
Key: this is my very very very
long string
️ 建议: 避免这样用。可能看起来很方便,但你可能会因为不小心使用了禁用的标点符号而搬起石头砸自己的脚,并引发语法错误。
双引号风格[6] (\和"必须被\转义,换行符可以用一个\n序列插入,行可以不带空格的\结尾连接)
结果是:
"this is my very very \"very\" loooong string.\n\nLove, YAML."
️ 建议: 仅在非常具体的情况下使用。这是唯一可以在不添加空格的情况下将一个很长的标记(如URL)跨行分隔的方法。也许在中间添加换行符是很有用的。
单引号风格[7] (Literal ' 必须是成对的,没有特殊字符,可能用于表达以双引号开头的字符串):
Key: 'this is my very very "very"
long string, isn''t it.'
结果是: "this is my very very \"very\" long string, isn't it."
️ 建议: 避免。很少有好处,大多是不便。
带 缩进指示器的块样式
如果上面的内容对你来说还不够,你可以添加一个“块缩进指示符[8]”(如果你有块咬合指示符的话,在你的块咬合指示符之后):
- >8
My long string
starts over here
- |+1
This one
starts here
注:前导空格采用折叠样式(>)
如果你以折叠的方式在非首行开始插入额外的空格,它们将被保留,并带有额外的换行符。(这种情况不会发生在流样式上。)第6.5节[9]:
️ Reference: 此外,折叠不适用于包含前导空格的文本行周围的换行符。注意,这种更缩进的行可能只包含这样的前导空格。
- >
my long
string
many spaces above
- my long
string
many spaces above
结果是: ["my long\n string\n \nmany spaces above\n","my long string\nmany spaces above"]
总结
在该表中,_表示空格字符。\n表示“换行符”(在JavaScript中为\n),“其他功能”下除外。“前导空格”应用于第一行之后(建立缩进):
示例🥴🥴🥴
注意“空格”之前一行的末尾空格。
- >
very "long"
'string' with
paragraph gap, \n and
spaces.
- |
very "long"
'string' with
paragraph gap, \n and
spaces.
- very "long"
'string' with
paragraph gap, \n and
spaces.
- "very \"long\"
'string' with
paragraph gap, \n and
s\
p\
a\
c\
e\
s."
- 'very "long"
''string'' with
paragraph gap, \n and
spaces.'
- >-
very "long"
'string' with
paragraph gap, \n and
spaces.
[
"very \"long\" 'string' with\nparagraph gap, \\n and spaces.\n",
"very \"long\"\n'string' with\n\nparagraph gap, \\n and \nspaces.\n",
"very \"long\" 'string' with\nparagraph gap, \\n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \\n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \\n and spaces."
]
References
[1] 折叠样式: https://yaml.org/spec/1.2/#style/block/folded [2] 字面风格: https://yaml.org/spec/1.2/#style/block/literal [3] YAML规范1.2: https://yaml.org/spec/1.2/#id2760844 [4] 块咬合指示符: https://yaml.org/spec/1.2/#chomping// [5] plain style: https://yaml.org/spec/1.2/#style/flow/plain [6] 双引号风格: https://yaml.org/spec/1.2/#style/flow/double-quoted [7] 单引号风格: https://yaml.org/spec/1.2/#style/flow/single-quoted [8] 块缩进指示符: https://yaml.org/spec/1.2/#id2793979 [9] 第6.5节: https://yaml.org/spec/1.2/#id2779048
版权声明
本文为[东风微鸣]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/1985817
边栏推荐
- Bootsrap 模态框文字超出处理
- 【写作】论文写作技巧
- Computer Vision L7 -- Self-supervised Learning
- TcaplusDB君 · 行业新闻汇编(四)
- A young man's first acquaintance
- Redis optimization series (I) building redis master-slave based on docker
- Face recognition (5) face detection based on mctnn (pytoch)
- Android eye protection function, double non undergraduate byte beating Android interview question sharing
- 【深入理解TcaplusDB技术】读取列表指定位置数据接口说明——[List表]
- Raspberry Pi B的UART极简例程
猜你喜欢

WxWidgets learning notes (I): Interpretation of code:: blocks template project source code

学校固定资产管理系统由谁来做,云呐RFID固定资产管理系统

人脸识别 (5) 基于MCTNN人脸检测(Pytorch)

多款无源探头1:1衰减比有什么区别
![[in depth understanding of tcallusdb technology] example code - data batch return](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[in depth understanding of tcallusdb technology] example code - data batch return

人脸识别 (4) 人脸对齐

Face recognition (5) face detection based on mctnn (pytoch)

vscode处理代码合并冲突

About STS access control of Alibaba cloud OSS resources

Web automation testing framework realizes the operation of web elements through JS
随机推荐
Redis gets all the elements in the list
如何选择电流钳或电流探头
【深入理解TcaplusDB技术】示例代码——异步调用接口
Merge two ordered linked lists -- leetcode
[C language] learn file operation in 10 minutes
这两种人是做不好自媒体的,一辈子都赚不到大钱
数学——贝塞尔曲线
2022 operation of simulated test question bank and simulated test platform for main principals of hazardous chemicals business units
2022 tea artist (intermediate) examination questions and answers
Redis series 3: ThinkPHP uses redis
Web automation testing framework realizes the operation of web elements through JS
分布式任務調度平臺XXL-JOB安裝及使用
[in depth understanding of tcallusdb technology] description of data interface of designated location in replacement list - [list table]
中一科技深交所IPO:市值83亿 湖北诞生又一家上市企业
Computer Vision L7 -- Self-supervised Learning
2022年危险化学品生产单位安全生产管理人员考试题模拟考试平台操作
Face recognition (4) face alignment
【深入理解TcaplusDB技术】读取列表所有数据示例代码——[List表]
SVG系列——3,强大的路径
合并两个有序链表——LeetCode