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