Convert YAML file to Dictionary in Python - Introduction
YAML is a digestible data serialization language that is often used to create configuration files with any programming language. It is designed for human interaction. YAML is a strict superset of JSON which is also a data serialization language. As it is a strict superset, it can do everything that JSON can and more.
The advantage of using YAML files are that they portable across programming languages, extensive and support Unicode characters. It is more human-readable than other markup files like XML or even HTML for that matter. It supports a wide range of
data types, including maps, lists and scalars.
Many times in our application, we will need to write YAML file to
python dictionary, or convert YAML file to dictionary in Python. We can accomplish this task using
pyyaml library in Python.
Installing PyYAML Library
PyYAML is a YAML parser and emitter for Python. We will use this library to parse YAML file. You can install with the following command on the windows/linux system.
For
linux system
$ sudo pip install pyyaml
For windows system
pip install pyyaml
This will download and
install python library to work on YAML files.
Sample YAML file
This is a sample file saved as
exampleyaml.yaml for network settings.
instance:
Id: id2341
environment: us-east
serverId: s1342
awsHostname: 192.168.0.10
serverName: www.golinuxcloud.com
ipAddr: 192.168.0.1
roles: [webserver,php]
Different python functions used to convert YAML file to dictionary
Here, we will open and
read YAML file and load it in the dictionary using the functions of PyYAML library. We will use following functions to open and load a YAML files.
- open - This function will open the file in read mode and store it as a stream.
- yaml.safe_load - This function converts a YAML document to a Python object. It will accept a byte string, a Unicode string, an open binary file object, or an open text file object as a parameter.
- yaml.load_all - Alternatively, we can also use load all function to load multiple yaml file together.
Apart from this, there are four loaders available for the
load()
function as shown below.
- BaseLoader - Loads all the basic YAML scalars as Strings
- SafeLoader - Loads subset of the YAML safely. It is mainly used if the input is from an untrusted source.
- FullLoader - Loads the full YAML but avoids arbitrary code execution. It will pose a potential risk when used for the untrusted input.
- UnsafeLoader - Original loader for untrusted inputs and generally used for backward compatibility.
Examples to convert YAML file to dictionary
Example 1 : Using safe_load function
In this example, we will open the file and save it in the stream
variable. Thereafter, we will use stream to
safe_load
the yaml file in to the python object.
Firstly, we will import
pyyaml
as
yaml
library. Then we open the
data.yaml
file using the
open()
function and use
yaml.safe_load()
function. We can also use
yaml.load()
function to load YAML file. The
safe_load
function will prevent python from executing any arbitrary code in the YAML file.
Once the file is loaded we can display or process its values as per our requirement. The loaded YAML file works like a python object and we can reference its elements using keys.
# Program to convert yaml file to dictionary
import yaml
# opening a file
with open('exampleyaml.yaml', 'r') as stream:
try:
# Converts yaml document to python object
d=yaml.safe_load(stream)
# Printing dictionary
print(d)
except yaml.YAMLError as e:
print(e)
Output
{'instance': {'environment': 'us-east', 'roles': ['webserver', 'php'], 'awsHostname': '192.168.0.10', 'serverName': 'www.golinuxcloud.com', 'ipAddr': '192.168.0.1', 'serverId': 's1342', 'Id': 'id2341'}}
Example 2 : Using BaseLoader
The data file named as
data.yml
.
fruits :
name : Orange
color : Orange
type : Citrus
taste : Sweet/Sour
uses : Juice, pulp
In this example, we will open the file and save it in the stream variable. Thereafter, we will use
BaseLoader
to load the yaml file in to the python object.
# Program to convert yaml file to dictionary
import yaml
from yaml.loader import BaseLoader
# opening a file
with open('data.yaml', 'r') as stream:
try:
# Converts yaml document to python object
d=yaml.load(stream, Loader=BaseLoader)
for key, val in d.items():
print(key, " : ", val,"\n")
except yaml.YAMLError as e:
print(e)
Output
fruits : {'name': 'Orange', 'color': 'Orange', 'type': 'Citrus', 'taste': 'Sweet/Sour', 'uses': 'Juice, pulp'}
Example 3 : Using SafeLoader
The data file named as
data.yml
.
student:
name : Ram
dept : computer
location : Hyderabad
grade : 2
specialization : AI, ML
In this example, we will open the file and save it in the stream variable. Thereafter, we will use
SafeLoader
to load the yaml file in to the python object.
# Program to convert yaml file to dictionary
import yaml
from yaml.loader import SafeLoader
# opening a file
with open('data.yaml', 'r') as stream:
try:
# Converts yaml document to python object
d=yaml.load(stream, Loader=SafeLoader)
for key, val in d.items():
print(key, " : ", val,"\n")
except yaml.YAMLError as e:
print(e)
Output
student : {'name': 'Ram', 'dept': 'computer', 'location': 'Hyderabad', 'grade': 2, 'specialization': 'AI, ML'}
Example 4 : Using FullLoader
The data file named as
data.yml
.
student:
name : Ram
dept : computer
location : Hyderabad
grade : 2
specialization : AI, ML
In this example, we will open the file and save it in the stream variable. Thereafter, we will use FullLoader to load the yaml file in to the python object.
# Program to convert yaml file to dictionary
import yaml
from yaml.loader import FullLoader
# opening a file
with open('data.yaml', 'r') as stream:
try:
# Converts yaml document to python object
d=yaml.load(stream, Loader=FullLoader)
for key, val in d.items():
print(key, " : ", val,"\n")
except yaml.YAMLError as e:
print(e)
Output
student : {'name': 'Ram', 'dept': 'computer', 'location': 'Hyderabad', 'grade': 2, 'specialization': 'AI, ML'}
Example 5 : Using UnsafeLoader
The data file named as
data.yml
.
fruits :
name : Orange
color : Orange
type : Citrus
taste : Sweet/Sour
uses : Juice, pulp
In this example, we will open the file and save it in the stream variable. Thereafter, we will use
UnsafeLoader
to load the yaml file in to the python object.
# Program to convert yaml file to dictionary
import yaml
from yaml.loader import UnsafeLoader
# opening a file
with open('data.yaml', 'r') as stream:
try:
# Converts yaml document to python object
d=yaml.load(stream, Loader=UnsafeLoader)
for key, val in d.items():
print(key, " : ", val,"\n")
except yaml.YAMLError as e:
print(e)
Output
fruits : {'name': 'Orange', 'color': 'Orange', 'type': 'Citrus', 'taste': 'Sweet/Sour', 'uses': 'Juice, pulp'}
Summary
The knowledge of converting yaml file to dictionary is very useful while working on real time applications. In many situations, we will need to read and convert yaml file to dictionary so as to use the
key values stored in the dictionary for the further use. In this tutorial, we covered the functions and examples to convert yaml file to dictionary. We learned in detail about this with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on how to convert yaml file to
dictionary in Python.
References
PyYAMLDocumentation