gpt4 book ai didi

python - 使用通配符搜索迭代 Redis 哈希键

转载 作者:IT王子 更新时间:2023-10-29 06:10:56 25 4
gpt4 key购买 nike

我有 redis 键和这些键的值作为哈希集(键,值对)。我正在使用 python 来检索键值。例如:

top_link:files
key: file_path/foldername1
value: filename1

key: file_path/foldername2
value: filename2

key: test_path/foldername3
value: filename3

我想找出所有键名以“file_path”开头的哈希集键

我试过了

all_keys = redis_connection.hscan_iter("top_link:files")
for key in all_keys:
if key.startswith("file_path"):
redis_connection.hget("top_link:files",key)

有没有更好的方法来找到所有以“file_path”开头的散列键。 SCAN 似乎可以实现我想要实现的目标。但是所有示例都显示了对顶级键 (top_link:files) 的扫描,而不是对哈希键的扫描。有什么建议么?谢谢。

最佳答案

您可以在 hscan_iter 中提供一个 match 模式来获取成对的匹配键。通过 hscan_iter,您可以将键值对作为 元组。因此,您不必使用 hget 来获取值。

matched_pairs = redis_connection.hscan_iter('top_link:files', match='file_path*')
for keyvalue in matched_pairs:
# Here `keyvalue` is a tuple containing key and value
print keyvalue[0], keyvalue[1]

输出:

file_path/foldername2 filename2
file_path/foldername1 filename1

关于python - 使用通配符搜索迭代 Redis 哈希键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45558708/

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