Python Redis:如何解析列表结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15850112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redis: How to parse a list result
提问by treecoder
I am storing a list in Redis like this:
我在 Redis 中存储一个列表,如下所示:
redis.lpush('foo', [1,2,3,4,5,6,7,8,9])
And then I get the list back like this:
然后我像这样返回列表:
redis.lrange('foo', 0, -1)
and I get something like this:
我得到了这样的东西:
[b'[1, 2, 3, 4, 5, 6, 7, 8, 9]']
How can I convert this to actual Python list?
如何将其转换为实际的 Python 列表?
Also, I don't see anything defined in RESPONSE_CALLBACKSthat can help? Am I missing something?
另外,我没有看到任何RESPONSE_CALLBACKS可以帮助的定义?我错过了什么吗?
A possible solution (which in my opinion sucks) can be:
一个可能的解决方案(在我看来很糟糕)可以是:
result = redis.lrange('foo',0, -1)[0].decode()
result = result.strip('[]')
result = result.split(', ')
# lastly, if you know all your items in the list are integers
result = [int(x) for x in result]
UPDATE
更新
Ok, so I got the solution.
好的,所以我得到了解决方案。
Actually, the lpushfunction expects all the list items be passed as arguments and NOT as a single list. The function signature from redis-py source makes it clear...
实际上,该lpush函数期望将所有列表项作为参数传递,而不是作为单个列表传递。来自 redis-py 源的函数签名清楚地表明......
def lpush(self, name, *values):
"Push ``values`` onto the head of the list ``name``"
return self.execute_command('LPUSH', name, *values)
What I am doing above is send a single list as an argument, which is then sent to redis as a SINGLE item.
我在上面所做的是发送一个列表作为参数,然后将其作为单个项目发送到 redis。
I should be unpacking the list instead as suggested in the answer:
我应该按照答案中的建议打开列表:
redis.lpush('foo', *[1,2,3,4,5,6,7,8,9])
which returns the result I expect...
它返回我期望的结果......
redis.lrange('foo', 0, -1)
[b'9', b'8', b'7', b'6', b'5', b'4', b'3', b'2', b'1']
采纳答案by Jim Dennis
I think you're bumping into semantics which are similar to the distinction between list.append()and list.extend(). I know that this works for me:
我认为您遇到了类似于list.append()和list.extend()之间区别的语义。我知道这对我有用:
myredis.lpush('foo', *[1,2,3,4])
... note the * (map-over) operator prefixing the list!
... 注意列表前面的 * (map-over) 运算符!
回答by Yarkee
import json
r = [b'[1, 2, 3, 4, 5, 6, 7, 8, 9]']
rstr = r[0]
res_list = json.loads(rstr)
回答by Seperman
Another way: you can use RedisWorkslibrary.
另一种方式:您可以使用RedisWorks库。
pip install redisworks
pip install redisworks
>>> from redisworks import Root
>>> root = Root()
>>> root.foo = [1,2,3,4,5,6,7,8,9] # saves it to Redis as a list
...
>>> print(root.foo) # loads it from Redis later
It converts python types to Redis types and vice-versa. So even if you had nested list, it would have worked:
它将 python 类型转换为 Redis 类型,反之亦然。因此,即使您有嵌套列表,它也会起作用:
>>> root.sides = [10, [1, 2]] # saves it as list in Redis.
>>> print(root.sides) # loads it from Redis
[10, [1, 2]]
>>> type(root.sides[1])
<class 'list'>
Disclaimer: I wrote the library. Here is the code: https://github.com/seperman/redisworks
免责声明:我编写了库。这是代码:https: //github.com/seperman/redisworks

