Day15 of #90DaysOfDevOps Challenge
Aishwarya keshri
Azure DevOps Support Engineer at Microsoft || MS Certified - AZ900 || Ex-Onclusive || NIT Raipur
?Python Libraries:
The collection of modules used in Python while writing different programs is known as libraries. With the help of these libraries, we can write the most complex programs in Python.
As a DevOps engineer, when we are writing the automation scripts for the build, test, and deployment of our application, there are a lot of libraries available in Python which make our task possible-
?Tasks:
1??Task- Create a Dictionary in Python and write it to a JSON File.
Here I have used the JSON module to convert the dictionary to JSON. As we know the JSON format is very similar to the dictionary in Python. Both store the objects in key-value pair under {}.
Example:
import json
dict = { "Name" : "Anna", "Age" : "20", "ID" : "1" }
json_object = json.dumps(dict, indent = 1)
print(json_object)
2??Task - Read a json file?services.json?kept in the folder and print the service names of every cloud service provider.
output
aws : ec2
azure : VM
gcp : compute engine
There are two essential functions of the JSON module -
Here we will use json.load function to read the json file.
import json
with open("service.json") as jsonfile:
jsonObject = json.load(jsonfile)
jsonfile.close()
print(jsonObject)
print(output)
print("aws : ", jsonObject["services"]["aws"]["name"])
print("aws : ", jsonObject["services"]["azure"]["name"])
print("aws : ", jsonObject["services"]["gcp"]["name"])
领英推荐
Steps:
Services.json file -
Python script to read the json file and print the service names -
3??Task - Read the YAML file using Python, file?services.yaml,?and read the contents to convert yaml to json.
To convert the yaml file to json, we will first convert it to a Python dictionary and then to a json string.
Steps:
import json ?
import yaml
from yaml import SafeLoader ?
with open("service.yaml") as yaml_file: ?
py_dict = yaml.load(yaml_file, Loader=SafeLoader ) ? ? ?
json_object = json.dumps(py_dict, indent=4)
print("Yaml is converted to json", json_object)
service.yaml file -
Python script to read the Yaml file and convert it to json -
Thank you for reading! ??
Happy learning... ??