Python Libraries for DevOps
Reading JSON and YAML in Python
As a DevOps Engineer, you should be able to parse files, be it Txt, json, yaml, etc.
You should know what libraries one should use in Python for DevOps.
Python has numerous libraries like
os
,sys
,json
,yaml
etc that a DevOps Engineer uses in day-to-day tasks.
Click Here to refer to my github repo for reference for the following tasks :)
Tasks
- Create a Dictionary in Python and write it to a JSON File.
This code is the conversion of PYTHON →JSON using JSON.dumps()
import json
data_dict = {
1:"Linux",
2:"Git",
3:"Docker",
4:"Kubernetes",
5:"Terraform",
6:"Ansible",
7:"Chef"
}
json_object = json.dumps(data_dict, indent= 4)
print("Json format :\n",json_object)
- Read a json file
services.json
kept in this folder and print the service names of every cloud service provider.
Output:
YAML file using Python, file
services.yaml
and read the contents to convert yaml to jsonUse the PyYAML module’s
yaml.load()
function. This function parses and converts a YAML object to a Python dictionary
To install the pyYAML module :
pip install pyYAML
The below code will help us convert a yaml file into Python and then into Java
The output will be in JSON form
Subscribe to my newsletter
Read articles from Nikita Shinde directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by