英文:
How can I fix the 'AttributeError: 'int' object has no attribute 'items'' in Redis when using zadd() to add values to a sorted set?
问题
import redis
r = redis.StrictRedis(host="localhost", port=6379, db=0, charset="utf-8", decode_responses=True)
r.zadd("players", 10, "new_player")
"""
Traceback (most recent call last):
File "...\sorted_set.py", line 6, in
x = r.zadd("players", 10, "new_player")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...\AppData\Local\Programs\Python\Python311\Lib\site-packages\redis\commands\core.py", line 4109, in zadd
for pair in mapping.items():
^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'items'
"""
英文:
import redis
r= redis.StrictRedis(host="localhost", port=6379, db=0, charset="utf-8", decode_responses=True)
r.zadd("players", 10, "new_player")
"""
Traceback (most recent call last):
File "...\sorted_set.py", line 6, in <module>
x= r.zadd("players", 10, "new_player")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...\AppData\Local\Programs\Python\Python311\Lib\site-packages\redis\commands\core.py", line 4109, in zadd
for pair in mapping.items():
^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'items'
"""
答案1
得分: 1
如果您查看redis-py的文档,可以看到排序集的成员必须以字典形式提供。完整文档请参考这里。
示例代码如下:
import redis
r = redis.StrictRedis(host="localhost", port=6379, db=0)
x = r.zadd("players", {"new_player": 10})
英文:
If you check the documentation of redis-py, the members of the sorted set have to be provided as a dictionary. Full documentation here
The sample code would look like:
import redis
r = redis.StrictRedis(host="localhost", port=6379,db=0)
x = r.zadd("players", {"new_player":10})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论