Day - 15
Table of contents
Python Libraries for DevOps
Reading JSON and YAML in Python:
DevOps Engineers need to parse various file formats, including
txt, json
, andyaml
.Python offers essential libraries for DevOps tasks, such as
os
,sys
,json
, andyaml
.Proficiency in these libraries allows efficient handling and manipulation of different file types.
Navigating and extracting data from diverse file formats enhances a DevOps Engineer's effectiveness.
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
Answer:
- Read YAML file using python, file
services.yaml
and read the contents to convert yaml to json
- Read YAML file using python, file
Answer: The detailed steps to complete task3 are as follows.
1. Install prerequisites
Ensure Python 3 is installed
Install PyYAML module
pip install pyyaml
2. Create sample YAML file
In your code folder, create a file called
services.yaml
Add YAML content like:
3. Create python file & add below parts of code
Imports yaml and json modules
Loads data.yaml into yaml_data
Converts yaml_data to JSON string json_data
Writes json_data to new file data.json
Prints confirmation message
Output - The above code will print:
The services.json files contains:
Thanks for reaading my blog!
I'd love if you could share this article with anyone else who may find it useful!
Have a good day!
Subscribe to my newsletter
Read articles from Priya T directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by