Skip to content
Advertisement

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 empnasnselect e.id
                            as emp_idn       FROM employeesn
   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.

Advertisement

Answer

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 asnnselect e.id as emp_idnfrom employees as en'
'create or replace view empnasnselect e.id                        as emp_idn       FROM employeesn as en;'

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
    ;
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement