redis wrongtype在使用从scan\iter检索的键调用hgetall()时,使用redis py对持有错误类型值的键执行的操作

mspsb9vt  于 2021-06-09  发布在  Redis
关注(0)|答案(0)|浏览(318)

我有一个redis示例,它将数据存储为散列。哈希键看起来像“o:t:23:45”。我使用的是redis py,我可以得到一个密钥列表。

for key in rc.scan_iter():
    print(key)

b'o:t:49:15'b'o:t:50:156'b'o:t:51:159'b'o:t:52:1593'b'o:t:53:1591'
如果我执行rc.hgetall(b'o:t:53:1591'),我会得到正确的值。
但是,当我执行以下操作时:

for key in rc.scan_iter():
    rc.hgetall(key)

我得到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 2717, in hgetall
    return self.execute_command('HGETALL', name)
  File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 839, in execute_command
    return self.parse_response(conn, command_name,**options)
  File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 853, in parse_response
    response = connection.read_response()
  File "/usr/local/lib/python3.6/site-packages/redis/connection.py", line 705, in read_response
    raise response
redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

我试着把钥匙传给str:

for key in rc.scan_iter():
    rc.hgetall(str(key))

但是我得到的结果是空的。我错过了什么?是否可以遍历键列表并调用hgetall()来获取每个键的值?
编辑:正如itamar所指出的,数据库确实有一个键是字符串类型的条目。在hgetall之前进行类型检查可以避免此问题。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题