AttributeError: ‘int‘ object has no attribute ‘items‘
从命令行输入zadd week_charm:2022-4:542:zset a x可正常插入数据,于是准备用python批量插入数据,没想到直接报错了for id in ids.readlines():x = random.randint(1, 15) # 对应scorea = 1000000000000 - int(id.strip())# 对应valuer.zadd('week_charm:2
·
- 从命令行输入
zadd week_charm:2022-4:542:zset a x
可正常插入数据,于是准备用python批量插入数据,没想到直接报错了
for id in ids.readlines():
x = random.randint(1, 15) # 对应score
a = 1000000000000 - int(id.strip()) # 对应value
r.zadd('week_charm:2022-4:542:zset', a, x)
Traceback (most recent call last):
File "D:\test\week_star.py", line 17, in <module>
r.zadd('week_charm:2022-4:542:zset', a, x)
File "D:\test\venv\lib\site-packages\redis\commands\core.py", line 2749, in zadd
for pair in mapping.items():
AttributeError: 'int' object has no attribute 'items'
报错原因: int类型没有items属性
查看源码,发现需要传入2个参数,那么为有序集合的key,mapping要传入对应的值和比重,2个值不能分开传,只能用字典形式传入
于是改成r.zadd('week_charm:2022-4:542:zset', {x: a})
运行成功,通过 ZREVRANGE week_charm:2022-4:542:zset 0 -1 WITHSCORES
查看 值弄反了, 再次查看源码 发现 获取到对应的值后会先存到一个列表里,而且前后顺序进行了调整,所以需要将对应的参数进行跳转
redis命令: ZADD KEY_NAME SCORE1 VALUE1.. SCOREN VALUEN
最终版:r.zadd('week_charm:2022-4:542:zset', {a: x})
查看效果 运行成功,完美展示~
- 删除单一value或者score
zrem week_charm:2022-4:542:zset 11
- 删除整个key :
del week_charm:2022-4:542:zset
更多推荐
已为社区贡献3条内容
所有评论(0)