Save a yaml file to a generator object to a dictionary python

78282219

I am having a lot of issues saving a YAML file to a python dictionary. I will display two routes I take below, both being suboptimal.

Yaml file:

modules:
    module_1: True
    module_2: True
    module_3: False
scenarios:
    constant:
        start_date: "2018-09-30"
        end_date: "2019-09-30"
    adverse:
        start_date: "2019-09-30"
        end_date: "2022-09-30"

Route 1: Saving YAML file directly to a dictionary without specifying a loader which is now depreciated

import yaml
filepath = "C:\\user\\path\\file.yaml"
_dict = yaml.load(open(filepath))
print(type(_dict))
>>> <class 'dict'>

error message: Calling yaml.load() without loader=... is depreciated, as it is unsafe

Route 2: Loading in as a generator (which is not subscriptable)

import yaml
filepath = "C:\\user\\path\\file.yaml"
document = open(filepath, "r")
dictionary = yaml.safe_load_all(document)

print(type(dictionary)
>>> <generator object>

print(dictionary["modules"]["module_1"]
>>> generator object is not subscriptable

Is there a way I can import my yaml file into a dictionary safely? I wish to use the dictionary in my python project instead of creating global variables, etc.

Example:

if _dict["modules"]["module_1"]:
    # Do something
Adeom

Only calling without loader was depracted. You can always pass SafeLoader to the load function.

import yaml

with open(filepath, 'r') as stream:
    dictionary = yaml.load(stream, Loader=yaml.SafeLoader)

This should return your dictionary.

edit:

And as for yaml.safe_load_all, you only need to call generator.__next__() to obtain the dictionary.

import yaml
filepath = "C:\\user\\path\\file.yaml"
document = open(filepath, "r")
generator = yaml.safe_load_all(document)
dictionary = generator.__next__()

I would recomend the first option for your use.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python generator and file object

Edit yaml file and save it with python

Save dictionary to file Python Tkinter

Save an image from a Generator object - Python

How can I save a quanteda dictionary as a yaml file?

How to save a dictionary (the key to an object) in a file then read it

Issue in writing the dictionary into YAML file in python

Cannot save object to Python multiprocessing shared dictionary

Save a dictionary to a file (alternative to pickle) in Python?

How to make a dictionary in python and save data in a file

save array of object in file in python

Dictionary Generator in Python

Converting a YAML file to Python JSON object

How to parse/read a YAML file into a Python object?

Python: How do I save generator output into text file?

Convert Python dictionary to yaml

Loading empty dictionary when YAML file is empty (Python 3.4)

Load a YAML file and replace strings in resulting dictionary in Python

How do I write a yaml file from a dictionary with python?

How to read the yaml file as dictionary and update value using python

How do I configure multiple dictionary keys in a yaml file in Python?

python3 get nested dictionary/property from yaml file

Converting Python dictionary to YAML file with lists as multiline strings

Python, after save yaml file the formatting of a list elements is not proper

How to save a format like python list in yaml file

Save dictionary to file

File object (image) as value in a dictionary python

How to save a Python Dictionary as a Pickle object on AWS S3

Parsing yaml file and getting a dictionary