用 Redis 儲存 Python 字典
除了儲存單純的鍵值對之外,你也可以儲存更複雜的 Python 資料型別,例如 dict(字典)。在這個與 Redis 有關的最後一個練習中,你將使用 Python 的 redis 模組,練習如何儲存與取回 Python 字典。
和之前一樣,我們已建立連線物件並存入變數 redis_conn。祝你好運!
本練習屬於課程
NoSQL 入門
練習說明
- 使用變數
redis_conn,將字典london_weather_mapping儲存到 key"london_weather"。 - 使用變數
redis_conn與適當的方法,取回並列印 key"london_weather"的鍵值對。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a dictionary containing weather data
london_weather_mapping = {
"temperature": 42,
"humidity": 88,
"visibility": "low"
}
# Store the london_weather key-value pair
____.____(
"____",
mapping=____
)
# Retrieve and print the london_weather key-value pair
print(____.____("____"))