How can I fix the 'AttributeError: 'int' object has no attribute 'items'' in Redis when using zadd() to add values to a sorted set?

huangapple go评论59阅读模式
英文:

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=&quot;localhost&quot;, port=6379,db=0)
x = r.zadd(&quot;players&quot;, {&quot;new_player&quot;:10})

huangapple
  • 本文由 发表于 2023年5月22日 04:00:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定