Table of contents
#Day 15 of 90daysofdevops
Hi Friends,
Let's discuss libraries in Python. As a DevOps engineer, we should know how to parse the file. the file could be JSON or yaml or txt etc.,
Python has numerous libraries like os
, sys
, json
, yaml
etc that a DevOps Engineer uses in day-to-day tasks.
What is JSON?
JSON(JavaScript Object Notation) is a popular data format used for representing structured data. We can transmit our data over the network using either XML or JSON. For simpler data, we use JSON for the complexity we can use XML.
How to read JSON files?
Import Json module
Provide the file path which you want to read.(e.g. my_file="c:/ptyhon/myfile.json")
Open the file to read the content of a file e.g.open(my_file, "r").
Use json.load() function will convert JSON data into Python understandable format.
Tasks:
Create a Dictionary in Python and write it to a JSON File.
Read a JSON file
services.json
kept in this folder and print the service names of every cloud service provider.output aws : ec2 azure : VM gcp : compute engine
Read YAML file using Python, file
services.yaml
and read the contents to convert YAML to JSON
Here we have to read YAML file using Python. So we must have YAML module otherwise we can't read YAML file.
So before writing import yaml. Download YAML module otherwise your yaml code would not run.Here i have solved this isse.Just type below command:
pip install pyyaml
Here i am sharing screenshot for your reference.
Here we have finished our first task reading YAML file using python.
Now our next task is to convert YAML to JSON.
Thank you for reading this blog.Happy Learning!!!!!
You can follow me on LinkedIn for my daily updates:- linkedin.com/in/parimal-pradhan-b62021168
Great initiative by the #trainwithshubham community. Thank you Shubham Londhe