PyYAML variables in multi-line

I'm trying to get a multi-line comment for using variables in PyYAML, but not sure if this is possible.

So, in YAML

you can assign a variable like:

  current_host: &hostname myhost

      

But it doesn't expand as follows:

test: |
    Hello, this is my string
    which is running on *hostname

      

Is this possible, or will I have to use Python to parse it?

+3


source to share


2 answers


The mechanism of anchors ( &some_id

) and links ( *some_id

) is essentially intended to provide the ability to exchange full nodes between parts of a tree view that are YAML text. This, for example, is necessary in order to have the same complex element (sequence / list or collation / dict) that occurs in the list, is loaded twice as the same element (instead of two copies with the same values).

So yes, you need to do some parsing in Python. You can start with the mechanism I provided in this answer and change the test



   if node.value and node.value.startswith(self.d['escape'])

      

to find the escape character anywhere in the scalar and take appropriate action.

+3


source


You will find the answer here.



Just use +

between the lines and your lines must be enclosed in '

s.

-2


source







All Articles