Retrieving key-value data with Redis
Now that you've practiced storing key-value pairs with Redis and Python, it's time to explore the other half of that process: retrieving key-value pairs. In this example, you'll practice retrieving a few different key-value pairs that you might find in a web application built to report on weather data.
A connection object has been created and stored in the variable redis_conn
. Enjoy!
Diese Übung ist Teil des Kurses
Introduction to NoSQL
Anleitung zur Übung
- Use the
redis_conn
variable and the appropriate method to try and parse the temperature for eachcity
in the list ofcities
from Redis. - If the
temperature
value isNone
for the passed key, set"unknown temperature"
as the value for that key, and print a short message.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Loop through each of the cities
for city in cities:
# Grab the temperature
temperature = ____.___(f"{city}_temp")
# Check if the temperature is None
if temperature is None:
# Store an unknown temperature
____.____(f"{city}_temp", "unknown temperature")
print(f"Unknown temperature in {city}")
else:
# Otherwise, print the temperature
print(f"The temperature in {city} is {temperature}")