Search code examples
pythonpython-2.7yamlpyyaml

How to parse yaml file with string values


For the below example, I am trying to read this yaml file, parse it and write the contains under the data tag to a new yaml file. Using yaml.load() I am unable to differentiate between a string and int value.

Sample yaml file:

data:
    key1: "Value1"
    key2: Value2

My current parsing python code:

import io
import yaml

test_dict={}
with open("sample-string.yaml", "r") as stream:
    try:
        f = yaml.load(stream)
        test_dict['data'] = f['data']
    except yaml.YAMLError as exc:
        print(exc)

with io.open("output.yaml", "a",encoding="utf-8") as wf:
    try:
        yaml.dump(test_dict['data'],wf,allow_unicode=True,default_flow_style=False)
    except yaml.YAMLError as exc:
        print(exc)

The above file produces the below output:

key1: Value1
key2: Value2

As you can see the, the quotes are missing for the value of key1 i.e. it should be "Value1" instead of Value1. Any suggestions on how I can achieve this ?

Expected output

key1: "Value1"
key2: Value2

Solution

  • Is the quote part of the data, or just its representation? If it's part of the data, you'll have to indicate that in the yaml.

    data:
        key1: |
            "Value1"
        key2: Value2
    

    Note that enclosing quotes is optional on yaml string values, which means they must be explicitly included if they are to be part of the string data itself.

    # these two documents are identical
    data:
    - this
    - that
    - the other
    ---
    data:
    - "this"
    - "that"
    - "the other"