Table of contents
Python Library
Python library is a collection of related modules. It contains bundles of code that can be used repeatedly in different programs. It makes Python Programming simpler and more convenient for the programmer. A Python library is simply a collection of codes or modules of codes that we can use in a program for specific operations. Python has numerous libraries like os
, sys
, json
, yaml
etc.
JSON in Python
In Python, JSON data is represented as String. We use JSON module for this.
Below two methods is used to convert python to JSON-
i) dumps(): Converts Python file to JSON format without creating file
ii) dumps(): Converts Python file to JSON format by creating a file
Below two methods are used to convert JSON to python-
i) loads(): Convert JSON to python without reading from file
ii) load(): Convert JSON to python by reading from file. json.load()
takes a file object and returns the JSON object.
Tasks:
Create a Dictionary in Python and write it to a json File.
In the following code, we have imported json module using
import json
and created a dictionary namedfav_tools
. After that, we specified the file path where the converted data needs to be stored. We have used 'w' keyword to write in a file and the dump() function to convert JSON file to Python. Once the script is executed you'll find a file nameddata.json
in the same directory as your Python script, containing the JSON representation of the dictionary.
import json
fav_tools = {
"firstName":"John",
"lastName": "Smith",
"profession": "IT Engineer",
"age": 28,
"address": "Park Street, Kolkata",
}
# Specify the file path
file_path = "data.json"
# Write the dictionary to a JSON file
with open(file_path, "w") as json_file:
json.dump(fav_tools, json_file)
print("Dictionary written to JSON file successfully.")
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
Let's create a JSON file named services.json
on the same directory as your Python script-
{
"aws": "ec2",
"azure": "VM",
"gcp": "compute engine"
}
In the following code, we have specified the file path of the JSON file. We have used 'r' keyword to read the JSON file and load() function to convert JSON to Python and then printed the service names of each cloud service provider using for loop:
import json
# Read the JSON file
file_path = "services.json"
with open(file_path, "r") as json_file:
data = json.load(json_file)
# Print service names for each cloud service provider
for provider, service in data.items():
print(f"{provider} : {service}")
The Output is:
output
aws : ec2
azure : VM
gcp : compute engine
Read YAML file using python, file
services.yaml
and read the contents to convert yaml to jsonInstall the
pyyaml
library to access the YAML libraries.pip install pyyaml
Let's create a
services.yaml
file in the same directory-firstName: John lastName: Smith profession: IT Engineer age: 28 address: Park Street Kolkata
In the following code, we have used
import yaml
andimport json
to import yaml and json libraries and have specified the file path of the YAML file in file_path variable. We have used 'r' keyword to read the YAML file and the safe_load() function to convert YAML file to a Python dictionary and then dumps() function to convert the dictionary to JSON format with indentation. After the successful execution of the command, we can see the JSON representation of the YAML data printed in the console.
import yaml
import json
# Read the YAML file
file_path = "services.yaml"
#Convert YAML file to Python dictionary
with open(file_path, "r") as yaml_file:
yaml_data = yaml.safe_load(yaml_file)
# Convert YAML data to JSON
json_data = json.dumps(yaml_data, indent=4)
print(json_data)
Thanks for reading!
~Shilpi