gpt4 book ai didi

python - 如何使用 redis 存储和检索字典

转载 作者:IT老高 更新时间:2023-10-28 20:24:20 26 4
gpt4 key购买 nike

# 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

最佳答案

你可以通过hmset来实现(可以使用hmset设置多个key)。

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'}

关于python - 如何使用 redis 存储和检索字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32276493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com