About char b prefix in Python3.4.1 client connect to redis

前端 未结 2 649
忘了有多久
忘了有多久 2020-12-02 22:35

I am run into trouble .My code below.But I do not know why there is a char \'b\' before output string \"Hello Python\".

>>> import redis
>>>         


        
相关标签:
2条回答
  • 2020-12-02 22:50

    b'Hello Python' is a byte string - redis will auto-encode a unicode string for you on the way in, but it's your job to decode it on the way out.

    Better to explicitly encode and decode:

    >>> redisClient.set('test_redis', 'Hello Python'.encode('utf-8'))
    >>> redisClient.get('test_redis').decode('utf-8')
    'Hello Python'
    
    0 讨论(0)
  • 2020-12-02 23:11

    It means it's a byte string

    You can use:

    redis.StrictRedis(host="localhost", port=6379, charset="utf-8", decode_responses=True)
    

    using decode_responses=True to make a unicode string.

    0 讨论(0)
提交回复
热议问题