+1 vote
in Programming Languages by (3.9k points)
I want to read a ".yaml" / ".yml" file using Python. Which Python module should I use?

1 Answer

+2 votes
by (53.0k points)
selected by
 
Best answer

The Python module PyYAML can be used to process ".yml" or ".yaml" file. You can use the safe_load() function of this module to read data from the file. The safe_load() function returns a dictionary.

Here is an example:

import yaml

with open('data/inp.yml', 'r') as fyml:

    config = yaml.safe_load(fyml)

print(config)

In the "inp.yml" file, I have the following data:

XGB_params:

  n_jobs: 2

  max_depth: 3

  random_state: 101

The output of the above code is: 
{'XGB_params': {'n_jobs': 2, 'max_depth': 3, 'random_state': 101}}

Related questions


...