Converting Between Python Dictionaries and JSON

We will use json.dums() function to convert to json from python dictionary and json.loads() to convert from json to python dictionary.

In this example code we used flower details data in python dictionary and converted it to json.


import json

# flowers in python dictionary
flowers = {
    "rose": {
        "scientific_name": "Rosa",
        "grows_in": ["gardens", "wild"],
        "colors": ["red", "pink", "white"]
    },
    "sunflower": {
        "scientific_name": "Helianthus annuus",
        "grows_in": ["fields", "gardens"],
        "colors": ["yellow"]
    }
}

# Converting Python dictionary to JSON

flowers_json = json.dumps(flowers)
# converted to json
print(flowers_json)



# we can add indent to make it more readable
flowers_json = json.dumps(flowers, indent=4)
print(flowers_json)





# Now convert json to python dictionary
#fowers_json is alreay in json

flowers_dict = json.loads(flowers_json)
#converted to python dictionary
print(flowers_dict)



Exporting Python Dictionaries to JSON Files

we can also export this python dictionary data to json


import json

# flowers in python dictionary
flowers = {
    "rose": {
        "scientific_name": "Rosa",
        "grows_in": ["gardens", "wild"],
        "colors": ["red", "pink", "white"]
    },
    "sunflower": {
        "scientific_name": "Helianthus annuus",
        "grows_in": ["fields", "gardens"],
        "colors": ["yellow"]
    }
}

# Converting Python dictionary to JSON

flowers_json = json.dumps(flowers)

# Exporting Python dictionary to a JSON file
with open("flowers.json", "w") as json_file:
    json.dump(flowers, json_file, indent=4)


Loading JSON Data from Files into Python Dictionaries


# Loading JSON data from a file into Python dictionary
import json
with open("flowers.json", "r") as json_file:
	#convert to python dictionary with json.load()
    loaded_flowers_dictionary = json.load(json_file)

print(loaded_flowers_dictionary)



How to create, read and write csv file from python

First Lets talk about csv

Its a plain text format which comes from (Comma Separated Values)

Most spreadsheet application supports this. You can open and edit it from excl, Libra office cal , google sheets and other spread sheet application, also supported by most of the programming language.

For simplicity and flexibility often used in data science.

Create Our first csv file

We will create our csv file with as simple way as possible. Even we will not use any spreadsheet application.

Open any text editor. You can open vs code or geany or your beloved code editor.

In the text editor. create and save a new file named colors.cvs Make sure you properly added the .csv extinction.

Now add this text in the file.

Color,Red,Green,Blue,Hex Code
Red,255,0,0,#FF0000
Green,0,255,0,#00FF00
Blue,0,0,255,#0000FF
Yellow,255,255,0,#FFFF00
Orange,255,165,0,#FFA500
Purple,128,0,128,#800080
Cyan,0,255,255,#00FFFF
Magenta,255,0,255,#FF00FF
Pink,255,192,203,#FFC0CB
Lime,0,255,0,#00FF00
Teal,0,128,128,#008080
Brown,165,42,42,#A52A2A
Maroon,128,0,0,#800000
Navy,0,0,128,#000080
Olive,128,128,0,#808000
Sky Blue,135,206,235,#87CEEB
Gold,255,215,0,#FFD700
Silver,192,192,192,#C0C0C0

save it . That’s all.

Now we will read the csv file from python.

Before that we need to be clear about python file mode

Mode Description
‘r’ Read-only mode. (Read): Open a file to only read its contents.
‘w’ Write-only mode. (Write): Create a new file or completely rewrite an existing one. (erases old data!)
‘a’ (Append): The data is written to the end of the file.
‘r+,’ ‘w+,’ ‘a+’ Read/write mode. These allow both reading and writing, with variations on how existing data is handled

Python have build in csv module.

To read csv file we will use

csv.reader


csv.DictReader

csv.reader

  • Turns each row of your CSV file into a list of the individual values.
  • Example: [‘Sky Blue’, ‘135’, ‘206’, ‘235’, ‘#87CEEB’]

csv.DictReader

  • Turns each row of your CSV into a dictionary (like a little address book).
  • Uses the first row of the CSV as keys (headers) for the dictionaries.
  • Example: {‘Color’: ‘Sky Blue’, ‘Red’: ‘135’, ‘Green’: ‘206’, ‘Blue’: ‘235’, ‘Hex Code’: ‘#87CEEB’}

Now open our colors.csv with python

Example with csv.reader:

The csv.reader function turns each row of the CSV file into a list of individual values. The example



import csv

with open('colors.csv', 'r', newline='') as csvfile:
	reader = csv.reader(csvfile)
	next(reader)
	for row in reader:
		print(row)
		color_name = row[0]
		red_value = int(row[1])
		green_value = int(row[2])
		blue_value = int(row[3])
		hex_code = row[4]

		# We can now process the color data as needed
		print(f"Color: {color_name} (R: {red_value}, G: {green_value}, B: {blue_value}), Hex Code: {hex_code}")





csv.DictReader

The csv.DictReader function turns each row of the CSV file into a dictionary, using the first row as keys (headers). Example:



import csv

with open('colors.csv', 'r', newline='') as csvfile:
	reader = csv.DictReader(csvfile)


	for row in reader:
		print(row)
		color_name = row['Color']
		red_value = int(row['Red'])
		green_value = int(row['Green'])
		blue_value = int(row['Blue'])
		hex_code = row['Hex Code'] 

		print(f"Color: {color_name} (R: {red_value}, G: {green_value}, B: {blue_value}), Hex Code: {hex_code}")




csv writer

The csv.writer function allows you to write data from lists to a CSV file. Example:



import csv

colors_to_write = [
    ['White', 255, 255, 255, '#FFFFFF'],
    ['Black', 0, 0, 0, '#000000'],
    ['Gold', 255, 215, 0, '#FFD700']
]

with open('new_colors.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['Color', '255', '0', '0', '#FF0000'])  # Write the header row
    writer.writerows(colors_to_write)  # Write the data rows






CSV DictWriter

The csv.DictWriter function allows you to write data from dictionaries to a CSV file. Example:



import csv

colors_to_write = [
    {'Color': 'White', '255': 255, '0': 255, '#FF0000': '#FFFFFF'},
    {'Color': 'Black', '255': 0, '0': 0, '#FF0000': '#000000'},
    {'Color': 'Gold', '255': 255, '0': 215, '#FF0000': '#FFD700'},
] 

with open('new_colors_dict.csv', 'w', newline='') as csvfile:
    fieldnames = ['Color', '255', '0', '#FF0000'] 
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()  # Write the header row
    writer.writerows(colors_to_write)  # Write the data rows





Now a practical approach . Analysis our csv data



import csv
import matplotlib.pyplot as plt


filename = 'colors.csv'

red_values = []
green_values = []
blue_values = []

with open(filename, 'r', newline='') as csvfile:
	reader = csv.reader(csvfile)
	next(reader)
	for row in reader:
		red_values.append(int(row[1]))
		green_values.append(int(row[2]))
		blue_values.append(int(row[3]))


# Visualize histograms of red, green, and blue values
fig, axs = plt.subplots(3)
fig.suptitle('Histograms of RGB Values')
axs[0].hist(red_values, bins=20, color='red')
axs[0].set_xlabel('Red Value')
axs[0].set_ylabel('Frequency')
axs[1].hist(green_values, bins=20, color='green')
axs[1].set_xlabel('Green Value')
axs[1].set_ylabel('Frequency')
axs[2].hist(blue_values, bins=20, color='blue')
axs[2].set_xlabel('Blue Value')
axs[2].set_ylabel('Frequency')
plt.show()




Display Matplotlib Plots in Pygame: A Step-by-Step Guide

Our tasks for this

Import necessary module



import sys
import pygame
import matplotlib
from pygame.locals import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas



from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

This code imports a tool from Matplotlib that lets you save plots as image files (like PNG or JPG). Use this for embedding plots in websites, reports, or presentations. We will apply this to implement in pygame.

Initiate pygame and setup the display



pygame.init()

clock = pygame.time.Clock() 
display_screen = pygame.display.set_mode((1200, 600))
background_color = (144, 238, 144)


we will use pygame.time.Clock() to control the frame rate of this application

Create a Matplotlib figure and canvas



figure, axis = plt.subplots()
axis.plot([1, 2, 3, 4, 5, 6, 7], [10, 20, 25, 30, 25, 45, 50]) 
plot_canvas = FigureCanvas(figure)  # Create a canvas to render the Matplotlib plot 



  • figure, axis = plt.subplots()
    This sets up your plotting area. Imagine fig as a picture frame, and axis as the place inside it for our graph.

  • axis.plot([1, 2, 3, …], [10, 20, 25, …])
    This draws the line. The numbers are coordinates (like on a map) for the line’s points.

  • plot_canvas = FigureCanvas(figure)
    This line gets our plot ready to be turned into an image file if needed.”

Apply the drawing and update in the while loop



# Main game loop
running = True
while running:
    clock.tick(10)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    display_screen.fill(background_color)

    # Draw the Matplotlib plot onto the Pygame screen
    plot_canvas.draw()  # Update the Matplotlib plot if needed
    renderer = plot_canvas.get_renderer()  
    matplotlib_plot_rgba_image_data = renderer.tostring_rgb()  # Get raw image data of the plot
    plot_canvas_width, plot_canvas_height = plot_canvas.get_width_height() 

    # Convert the Matplotlib image data into a Pygame surface
    plot_surface = pygame.image.fromstring(matplotlib_plot_rgba_image_data, 
                                           (plot_canvas_width, plot_canvas_height), 
                                           "RGB")

    # Display the plot on the Pygame screen
    display_screen.blit(plot_surface, (300, 50))  # Place the plot at position (300, 50)

    pygame.display.update()


The complete Code



import sys
import pygame
import matplotlib
from pygame.locals import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

# Initialize Pygame
pygame.init()

# Set up the display window
clock = pygame.time.Clock()  # Used to control the speed of the game loop
display_screen = pygame.display.set_mode((1200, 600))  # Create a window 1200 pixels wide, 600 pixels tall
background_color = (144, 238, 144)  # A light green color

# Create the Matplotlib plot 
figure, axis = plt.subplots()  # Create a figure (the container) and an axis (for plotting)
axis.plot([1, 2, 3, 4, 5, 6, 7], [10, 20, 25, 30, 25, 45, 50])  # Plot sample data 
plot_canvas = FigureCanvas(figure)  # Create a canvas to render the Matplotlib plot 

# Main game loop
running = True
while running:
    clock.tick(10)  # Limit the loop to run 10 times per second

    # Handle events (like keyboard presses, mouse clicks)
    for event in pygame.event.get():
        if event.type == QUIT:  # Check if the user clicked the close button
            pygame.quit()
            sys.exit()

    # Clear the screen with the background color
    display_screen.fill(background_color)

    # Draw the Matplotlib plot onto the Pygame screen
    plot_canvas.draw()  # Update the Matplotlib plot if needed
    renderer = plot_canvas.get_renderer()  
    matplotlib_plot_rgba_image_data = renderer.tostring_rgb()  # Get raw image data of the plot
    plot_canvas_width, plot_canvas_height = plot_canvas.get_width_height() 

    # Convert the Matplotlib image data into a Pygame surface
    plot_surface = pygame.image.fromstring(matplotlib_plot_rgba_image_data, 
                                           (plot_canvas_width, plot_canvas_height), 
                                           "RGB")

    # Display the plot on the Pygame screen
    display_screen.blit(plot_surface, (300, 50))  # Place the plot at position (300, 50)

    # Update the display to show changes
    pygame.display.update()




More Pygame related Article

https://programmerbee.com/category/pygame/

More pygame example codes on github