+1 vote
in Programming Languages by (8.1k points)

I have a dictionary in text format and am using YAML to convert it into a dictionary. Although the string gets converted into the dictionary, YAML gives a warning for the following code:

>>> import yaml

>>> paramVals = "{'a':5,'b':6,'c':7}"

>>> yaml.load(paramVals)

__main__:1: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. 

{'a': 5, 'b': 6, 'c': 7}

How to get rid of this warning message? 

1 Answer

0 votes
by (15.9k points)

You can use one of the Loaders as a parameter to remove the warning message.

Replace

yaml.load(paramVals)

With

yaml.load(paramVals, Loader=yaml.FullLoader)

or

yaml.full_load(paramVals)

From the documentation of YAML,

The current Loader choices are:

  • BaseLoader: Only loads the most basic YAML
  • SafeLoader: Loads a subset of the YAML language, safely. This is recommended for loading untrusted input.
  • FullLoader: Loads the full YAML language. Avoids arbitrary code execution. This is currently (PyYAML 5.1) the default loader called by yaml.load(input) (after issuing the warning).
  • UnsafeLoader: (also called Loader for backwards compatability)

You may also use one of the shortcut "sugar" methods:

  • yaml.safe_load
  • yaml.full_load
  • yaml.unsafe_load
...