tags:

views:

1155

answers:

3

So I have two yaml files A and B and I want the contents of A to be inserted inside B, either spliced into the existing data structure (eg. an array), or as a child of an element, eg. the value for a certain hash key.

Is this possible at all? How? If not, any pointers to a normative reference?

A: 

http://yaml.org/type/yaml.html

Tag: tag:yaml.org,2002:yaml

+2  A: 

You could treat file A as a string inside file B. Here's an example inspired by a comment on another question that uses YAML's literal style for strings:

B.yaml: |
    - heading:
        name: A name
        taco: Yes
        age: 32
    - heading:
        name: Another name
        taco: No
        age: 27

When loaded, you'll get a mapping from "B.yaml" to the text of file B. Just load this resulting text to get the data in the second file out.

Here's how it looks in Python, using PyYAML:

from pprint import pprint
import yaml

fileA = """
    fileB: |
        - heading:
            name: A name
            taco: Yes
            age: 32
        - heading:
            name: Another name
            taco: No
            age: 27
"""

dataA = yaml.load(fileA)
dataB = yaml.load(dataA["fileB"])

pprint(dataA)   # A mapping to the text of the inner file
pprint(dataB)   # The data from that inner file
eksortso
Well that kinda misses the point.
kch
Thanks for the reply,, but how does this miss the point? I've made file B the value of the key "fileA" in my examples.What are you trying to do? I don't think YAML provides a way of explicitly referring to an external file, at least not in any explicit fashion.And if you're wanting to incorporate all elements of an existing YAML file in the data structure of another, you'll have to work around the header line and multiple documents and their indicators. By putting the whole thing in a string, you get the entire file.
eksortso