Python day-to-day tasks
Tasks
Create a Dictionary in Python and write it to a JSON File
simply create a dictionary in Python and convert this Python object into JSON object and write into the JSON file.
Json.dumps() - convert the python object into an equivalent JSON object and stores the result into a JSON file at the working directory.
Read a JSON file
services.json
kept in this folder and print the service names of every cloud service provider.json.load() : A Python dictionary is created and it is returned via the json.load() function.
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
Install PyYAML with pip install
import yaml statement will import the module.
the open method will view the file
yaml.safe_load() - Prases the given and returns a python object constructed from the first document in the stream.
Thank you for reading this article!
Happy Learning!