Redisで辞書を保存および取得する方法 質問する

Redisで辞書を保存および取得する方法 質問する
# I have the dictionary my_dict
my_dict = {
    'var1' : 5
    'var2' : 9
}
r = redis.StrictRedis()

my_dict を保存し、redis で取得するにはどうすればよいでしょうか。たとえば、次のコードは機能しません。

#Code that doesn't work
r.set('this_dict', my_dict)  # to store my_dict in this_dict
r.get('this_dict')  # to retrieve my_dict

ベストアンサー1

hmset( を使って複数のキーを設定することもできます)で実行できますhmset

hmset("RedisKey", dictionaryToSet)

import redis
conn = redis.Redis('localhost')

user = {"Name":"Pradeep", "Company":"SCTL", "Address":"Mumbai", "Location":"RCP"}

conn.hmset("pythonDict", user)

conn.hgetall("pythonDict")

{'Company': 'SCTL', 'Address': 'Mumbai', 'Location': 'RCP', 'Name': 'Pradeep'}

おすすめ記事