I am having the following problem of mapping documents within a YAML file to a dict
and properly mapping them.
I have the following YAML file, which represents a server [db.yml
]:
instanceId: i-aaaaaaaa
environment:us-east
serverId:someServer
awsHostname:ip-someip
serverName:somewebsite.com
ipAddr:192.168.0.1
roles:[webserver,php]
I load this YAML file, which I can do without any problems, I think I understand that.
instanceId = getInstanceId[]
stream = file['db.yml', 'r']
dict = yaml.load_all[stream]
for key in dict:
if key in dict == "instanceId":
print key, dict[key]
I'd like the logic to work like the following:
- load yaml, map to dict
- look in every dict in the document, if the
instanceId
matches that which was set bygetInstanceId[]
, then print out all of the keys and values for that document.
If I look at the map data structure from the command line, I get:
{'instanceId': 'i-aaaaaaaa environment:us-east serverId:someServer awsHostname:ip-someip serverName:someserver ipAddr:192.168.0.1 roles:[webserver,php]'}
I think I might be creating the data structure for the YAML file improperly, and on matching the contents on the dict
, I am a bit lost.
Side note: I cannot load all of the documents in this file using yaml.load[]
, I tried yaml.load_all[]
, which seems to work but my main issue still exists.
In this article, we are going to see how to convert YAML to Dictionary in Python with the help of examples. Most of the time as a developer your requirement is that convert any YAML file to a Python dictionary, Then in that situation, you have an option to convert any YAML document or any YAML file to a Python dictionary. Python provides an external module pyyaml that is capable of converting YAML to Dictionary.
Contents
- 1 Prerequisites
- 2 What is YAML
- 3 How to Convert YAML to Dictionary in Python
- 4 Conclusion
Prerequisites
Python provides two types of
packages first is built-in packages that come with Python by default and the second is external packages that are installed using the pip command. So, to convert YAML to Dictionary in Python we will use pyyaml
packages that come under the external package that means you have to install it by using the pip
command.
pip install pyyaml
What is YAML
YAML stands for Yet Another Markup Langauge. It is a human-readable data
serialization language that is mainly used for writing configuration files where data is being stored or transmitted.
It is becoming very popular in the last few years because it provides an intuitive serialize manner to make build configuration files.
You can see below the document that is an example of YAML.
YAML Document:
name: "Vishvajit"
age: 23
address: Noida
Occupation: Software Developer
Skills:
- Python
- Django
- Flask
- FastAPI
- DRF [ Django Rest Framework ]
How to Convert YAML to Dictionary in Python
Here we will convert YAML to Python Dictionary in two ways first it uses a simple YAML string and another is from the YAML file.
Convert YAML to Python Dictionary
In this section, we will see the conversion of the YAML string to a Python dictionary.
Example: Convert YAML to Python Dictionary
import yaml
from pprint import pprint
#yaml document
yaml_document = """
name: "Vishvajit"
age: 23
address: Noida
Occupation: Software Developer
Skills:
- Python
- Django
- Flask
- FastAPI
- DRF [ Django Rest Framework ]
"""
# convert yaml document to dict
data = yaml.load[yaml_document, Loader=yaml.Loader]
pprint[data]
Output
{'Occupation': 'Software Developer',
'Skills': ['Python',
'Django',
'Flask',
'FastAPI',
'DRF [ Django Rest Framework ]'],
'address': 'Noida',
'age': 23,
'name': 'Vishvajit'}
Convert YAML file to Python Dictionary
In this section, you will see how to convert the YAML file to Dictionary in Python using Python pyyaml package.
Students.yaml
---
data:
- name: "Vishvajit"
age: 23
address: Noida
Occupation: Software Developer
Skills:
- Python
- Django
- Flask
- FastAPI
- DRF [ Django Rest Framework ]
- name: "John"
age: 30
address: UK
Occupation: front-end Developer
Skills:
- HTML
- JavaScript
- React
- Angular
- Nodejs
Example: Convert YAML file to Python Dictionary
import yaml
from pprint import pprint
with open["students.yaml", "r"] as file:
data = yaml.load[file, Loader=yaml.FullLoader]
pprint[data]
Output
{'data': [{'Occupation': 'Software Developer',
'Skills': ['Python',
'Django',
'Flask',
'FastAPI',
'DRF [ Django Rest Framework ]'],
'address': 'Noida',
'age': 23,
'name': 'Vishvajit'},
{'Occupation': 'front-end Developer',
'Skills': ['HTML', 'JavaScript', 'React', 'Angular', 'Nodejs'],
'address': 'UK',
'age': 30,
'name': 'John'}]}
Conclusion
So, In this article, we have seen all about how to convert YAML to Dictionary in Python. Most of the time as a developer you have a YAML file and you want to convert it into Python native data type dictionary, Then you can follow the above approach.
I hope this article will help you to convert YAML to Python Dictionary, If you like this article, please share and keep visiting for further Python tutorials.
Related Articles:-
- How to convert Dictionary to Excel in Python
- How to convert Excel to Dictionary in Python
- How to convert string to DateTime in Python
- How to sort the list of Dictionaries in Python
PyYaml documentation:- Click Here
Thanks for reading. 👏👏
About the Author: Admin
Programming Funda aims to provide the best programming tutorials to all programmers. All tutorials are designed for beginners as well as professionals.
Programming Funda explains any programming article well with easy examples so that you
programmer can easily understand what is really going on here.
View all post by Admin | Website