Python-redis keys() returns list of bytes objects instead of strings

前端 未结 4 1613
孤独总比滥情好
孤独总比滥情好 2021-02-01 14:37

I\'m using the regular redis package in order to connect my Python code to my Redis server.

As part of my code I check if a string object is existed in my R

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 15:03

    One solution can be:

    decode the redis key

    print(key)
    #output is : b'some_key'
    
    print(key.decode())
    #output is : 'some_key'
    

    Updated :

    Pass dictionary object into RedisPost class then decoding individual item and storing them as a object.

    class RedisPost():
       def __init__(self, dic):
          for k,v in dic.items():
              if not isinstance(k,int):
                 var = k.decode()
                 setattr(self,var,v.decode())
    
    
    my_dic = {'a':12, 'b':123}
    obj = RedisPost(my_dic)
    print(obj.a) # output will be 12 
    

提交回复
热议问题