Force YAML values to be strings

Viewed 2769

Look at this code, under Python 2.7:

>>> import yaml
>>> yaml.load('string: 01')
{'string': 1}
>>> :(

Is it possible to obtain the string 01 without modifying the yaml file? I didn't find anything in the docs.

2 Answers

I was seeking for exactly the opposite effect: Numbers where being converted to stings, but numbers where wanted. I was using accidentally the BaseLoader (Dame copy-paste!).

The LOADER is the answer, as already stated by @Konrad Hałas.

Force Strings:

yaml.load('string: 01', Loader=yaml.loader.BaseLoader)

Force Number: (Default)

yaml.load('string: 01', Loader=yaml.loader.SafeLoader)

Link about Deprecation and detail:

https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation

Related