Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force YAML values to be strings

Tags:

python

yaml

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.

like image 625
Jill-Jênn Vie Avatar asked Aug 17 '12 20:08

Jill-Jênn Vie


People also ask

Should YAML values be in quotes?

For most scalars you don't need any quotes at all, but if you need to define some piece of data which contains characters that could be mistaken with YAML syntax you need to quote it in either double " or single ' quotes for the YAML file to stay valid.

Do strings need to be quoted in YAML?

Quotes are required when the string contains special or reserved characters. The double-quoted style provides a way to express arbitrary strings, by using \ to escape characters and sequences. For instance, it is very useful when you need to embed a \n or a Unicode character in a string.

What is |- in YAML?

The default, clip, puts a single newline at the end of the string. To remove all newlines, strip them by putting a minus sign ( - ) after the style indicator.


1 Answers

Try:

>> import yaml
>> yaml.load('string: 01', Loader=yaml.loader.BaseLoader)
{u'string': u'01'}
like image 196
Konrad Hałas Avatar answered Sep 25 '22 06:09

Konrad Hałas