Redis로 키-값 데이터 가져오기
이제 Redis와 Python으로 키-값 쌍을 저장하는 연습을 마쳤으니, 그 과정의 다른 절반인 키-값 쌍을 가져오는 방법을 살펴보겠습니다. 이 예제에서는 날씨 데이터를 리포팅하는 웹 애플리케이션에서 볼 법한 몇 가지 키-값 쌍을 가져오는 연습을 하게 됩니다.
연결 객체는 이미 생성되어 redis_conn 변수에 저장되어 있습니다. 시작해 보세요!
이 연습은 강의의 일부입니다
NoSQL 입문
연습 안내
redis_conn변수와 적절한 메서드를 사용해 Redis에서cities목록의 각city에 대한 온도를 가져오세요.- 전달된 키의
temperature값이None이면 해당 키의 값으로"unknown temperature"를 설정하고, 짧은 메시지를 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# 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}")