Fancier Output Formatting in Python

Maybe something related to working with CSV files or JSON data? And could you also explain how to use string interpolation for nested dictionaries and lists?
Sure thing, Let’s roll with that. When it comes to working with CSV (Comma Separated Values) files, the `csv` module in Python is a great tool to have in your arsenal. It allows you to read and write data from/to CSV files easily. Here’s an example of how to use string formatting to print out some nicely-formatted output when reading a CSV file:

# Import the csv module
import csv

# Open the data.csv file in read mode and assign it to the variable 'f'
with open('data.csv', 'r') as f:
    # Use the csv.reader() function to read the file and assign it to the variable 'reader'
    reader = csv.reader(f)
    # Use a for loop to iterate through each row in the file
    for row in reader:
        # Use string formatting to print out the data in a nicely-formatted way
        # The first element in each row is the name, and the second element is the age
        print("Name: {},\tAge: {}".format(row[0], row[1]))

In this example, we’re using a `csv.reader()` object to read from our CSV file and iterate over each line (i.e., row). We then use string formatting with the `format()` method to print out nicely-formatted output for each row. The `{}` placeholders are replaced by the values of the first two elements in the current row, which we access using indexing.
Now JSON data. When working with JSON (JavaScript Object Notation) data, you might want to format it nicely when printing it out for debugging purposes or displaying it on a web page. Here’s an example of how to use string formatting to print out some nicely-formatted output from a JSON object:

# Importing the json module
import json

# Creating a dictionary with some data
data = {
    "name": "John Doe",
    "age": 30,
    "address": {
        "city": "San Francisco",
        "state": "CA"
    }
}

# Using the json.dumps() function to convert the dictionary into a JSON string with indentation of 4 spaces
formatted_data = json.dumps(data, indent=4)

# Printing the formatted data
print(formatted_data)

# Output:
# {
#     "name": "John Doe",
#     "age": 30,
#     "address": {
#         "city": "San Francisco",
#         "state": "CA"
#     }
# }

# Explanation:
# The json module is imported to work with JSON data.
# A dictionary named "data" is created with some sample data.
# The json.dumps() function is used to convert the dictionary into a JSON string with indentation of 4 spaces.
# The formatted data is stored in a variable named "formatted_data".
# The print() function is used to display the formatted data on the console.

In this example, we’re using Python’s built-in `json` module to convert our dictionary (i.e., JSON object) into a string representation of it. We then use the `indent` parameter with the `json.dumps()` function to format the output nicely for readability purposes.
Finally, how to use string interpolation for nested dictionaries and lists. This can be tricky because you need to access elements within a dictionary or list using indexing or key lookup. Here’s an example of how to do this:

# Define a dictionary named "data" with key-value pairs
data = {
    "name": "John Doe",
    "age": 30,
    "address": {
        "city": "San Francisco",
        "state": "CA"
    }
}

# Use the `str.format()` function to format the output nicely for readability purposes
# Print the name and age from the "data" dictionary using string formatting
print("Name: {}, Age: {}".format(data["name"], data["age"]))

# Print the address from the "data" dictionary using string formatting and nested dictionary/list access
# Use "\n" to create a new line and "\t" to add a tab for better formatting
print("Address:\n\tCity: {},\tState: {}".format(data["address"]["city"], data["address"]["state"]))

# Output:
# Name: John Doe, Age: 30
# Address:
#   City: San Francisco,   State: CA

In this example, we’re using string formatting to print out nicely-formatted output for the name and age of our JSON object. We then use nested dictionary/list access to format the address information in a more readable way by printing it on multiple lines with indentation.

SICORPS