jun
jun

Reputation: 143

Difference of Double Quotes and Vertical bar in yaml

I'm writing a Python script that creates a YAML file according to DataFrame and I came across this:

test:
   query: |
      create or replace view emp as

      select e.id as emp_id
      from employees as e

vs

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

Are they technically the same or am I missing something? If they are not the same, how do I make the second version like the first that uses a vertical bar.

Upvotes: 1

Views: 293

Answers (1)

Anthon
Anthon

Reputation: 76902

They are technically not the same, but they are similar, as you can see by loading them:

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']))

which gives:

'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;'

The double quoted style can represent any string using backslash escapes, but is not always very readable.

If you have a string with just linebreaks, you can often use the first form, called a literal style scalar.

You can create individual literal style scalars using the following (of course you don't have to start from loaded data, you can just as well create that in 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)

which gives:

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

Upvotes: 1

Related Questions