python read yaml code example
Example 1: python read yaml
import yaml
with open(r'E:\data\categories.yaml') as file:
documents = yaml.full_load(file)
for item, doc in documents.items():
print(item, ":", doc)
Example 2: python write yaml
import yaml
dict_file = [{'sports' : ['soccer', 'football', 'basketball', 'cricket', 'hockey', 'table tennis']},
{'countries' : ['Pakistan', 'USA', 'India', 'China', 'Germany', 'France', 'Spain']}]
with open(r'E:\data\store_file.yaml', 'w') as file:
documents = yaml.dump(dict_file, file)
Example 3: read data from yaml file in python
import yaml
with open("data.yaml", 'r') as stream:
data_loaded = yaml.safe_load(stream)
Example 4: read yml file in python
{'a_key': 'a_value', 'another_key': 'another_value', 'nested_dictionary': {'nested_key': 'nested_value'}}