简体   繁体   中英

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 ie it should be "Value1" instead of Value1 . Any suggestions on how I can achieve this ?

Expected output

key1: "Value1"
key2: Value2

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM