yaml中双引号和竖线的区别

Difference of Double Quotes and Vertical bar in yaml

我正在编写一个 Python 脚本来根据 DataFrame 创建一个 YAML 文件,我遇到了这个:

test:
   query: |
      create or replace view emp as

      select e.id as emp_id
      from employees as e

test:
   query: "create or replace view emp\nas\nselect e.id\
    \                        as emp_id\n       FROM employees\n
   as e\
   \n;"

它们在技术上是相同的还是我遗漏了什么?如果它们不一样,我如何使第二个版本像第一个使用竖线的版本一样。

它们在技术上并不相同,但它们很相似,加载它们可以看到:

import ruamel.yaml

yaml = ruamel.yaml.YAML(typ='safe', pure=True)
for fn in 'literal.yaml', 'quoted.yaml':
    data = yaml.load(Path(fn))
    print(repr(data['test']['query']))

给出:

'create or replace view emp as\n\nselect e.id as emp_id\nfrom employees as e\n'
'create or replace view emp\nas\nselect e.id                        as emp_id\n       FROM employees\n as e\n;'

双引号样式可以表示任何使用反斜杠转义的字符串,但并不总是很易读。

如果您有一个只有换行符的字符串,您通常可以使用第一种形式,称为 literal 样式标量.

您可以使用以下方法创建单独的文字样式标量(当然您不必从 加载的数据,您也可以在 YAML 中创建):

import sys
import ruamel.yaml
from ruamel.yaml.scalarstring import LiteralScalarString as LS

yaml = ruamel.yaml.YAML()
data = yaml.load(Path('quoted.yaml'))
data['test']['query'] =  LS(data['test']['query'])
yaml.dump(data, sys.stdout)

给出:

test:
  query: |-
    create or replace view emp
    as
    select e.id                        as emp_id
           FROM employees
     as e
    ;