In this tutorial, We are going to learn yaml parsing in Python examples
How to install the yaml package for Python?
There are many libraries available to parse yaml in Python First, Search for yaml packages using the pi search command
pip search yaml
This lists the following available packages
- PyYAML
- yamltools
- PySyck
[pyyaml](https://pyyaml.org/wiki/PyYAMLDocumentation)
is an up-to-date python yaml module for reading and writing yaml files.
First, install thepyyaml
library using the pip
package manager
In windows,
pip install pyyaml
In Linux or Ubuntu
sudo apt-get install PyYAML // ubuntu
sudo yum install PyYAML //Linux
sudo aptitude install // debinal OS flavor
How to read YAML file in python
Let’s see multiple examples to read the yaml file and store it in an object.
Sample Yaml file read example
yaml
is a superset of json
. It contains key
and value
pairs with included indentation and tabs.
Given the config.yaml example file with database dictionary configuration details.
--- # Application configuration - config.yaml
author: Franc
database:
driver: com.mysql.jdbc.Driver
port: 3306
dbname: mydb
username: root
password:
Here is an example code to read a yaml file into an object
import yaml
with open('config.yaml') as file:
try:
databaseConfig = yaml.safe_load(file)
print(databaseConfig)
except yaml.YAMLError as exc:
print(exc)
Output:
{'author': 'Franc', 'database': {'driver': 'com.mysql.jdbc.Driver', 'port': 3306, 'dbname': 'mydb', 'username': 'root', 'password': None}}
Here are steps
- First import yaml module using
import
statement - Read the file using the open method
- safe_load method read the file content and converts it to a dictionary python object
- enclose file reading try and expect the block to hand exceptions
Let’s see another example for reading an array of yaml data
python parser to read an array of strings yaml data example
A sample yaml file - data.yaml which contains an array of strings
weekday:
- monday
- tuesday
- wednesday
- thursday
- friday
- saturday
- sunday
months:
- jan
- feb
- mar
- apr
- may
- june
- july
Let’s see a python code
import yaml
with open('data.yaml') as file:
try:
data = yaml.safe_load(file)
for key, value in data.items():
print(key, ":", value)
except yaml.YAMLError as exception:
print(exception)
Output:
weekday : ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
months : ['jan', 'feb', 'mar', 'apr', 'may', 'june', 'july']
Sequence of Steps
- import yaml module
- read file and store data in the data variable
- Iterate keys and values using for-loop
- print key and values
- enclose try and except block for yaml file read
Write dictionary into yaml file in python
- load yaml module into a Python file
- Declare a dictionary of objects with data
- Open file for writing data
- yaml module dump method writes data to a file
Here is an example to write a yaml dictionary into a file
import yaml
days = [{'weekday' : ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']},
{'months' : ['jan', 'feb', 'mar', 'apr', 'may', 'june', 'july']}]
with open(r'output.yaml', 'w') as file:
outputs = yaml.dump(days, file)
prints outputs
- weekday:
- monday
- tuesday
- wednesday
- thursday
- friday
- saturday
- sunday
- months:
- jan
- feb
- mar
- apr
- may
- june
- july