python - Redis: How to parse a list result -
i storing list in redis this:
redis.lpush('foo', [1,2,3,4,5,6,7,8,9]) and list this:
redis.lrange('foo', 0, -1) and this:
[b'[1, 2, 3, 4, 5, 6, 7, 8, 9]'] how can convert actual python list?
also, don't see defined in response_callbacks can help? missing something?
a possible solution (which in opinion sucks) can be:
result = redis.lrange('foo',0, -1)[0].decode() result = result.strip('[]') result = result.split(', ') # lastly, if know items in list integers result = [int(x) x in result] update
ok, got solution.
actually, lpush function expects list items passed arguments , not single list. function signature redis-py source makes clear...
def lpush(self, name, *values): "push ``values`` onto head of list ``name``" return self.execute_command('lpush', name, *values) what doing above send single list argument, sent redis single item.
i should unpacking list instead suggested in answer:
redis.lpush('foo', *[1,2,3,4,5,6,7,8,9]) which returns result expect...
redis.lrange('foo', 0, -1) [b'9', b'8', b'7', b'6', b'5', b'4', b'3', b'2', b'1']
i think you're bumping semantics similar distinction between list.append() , list.extend(). know works me:
myredis.lpush('foo', *[1,2,3,4]) ... note * (map-over) operator prefixing list!
Comments
Post a Comment