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

前端 未结 2 672
忘了有多久
忘了有多久 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'
    

提交回复
热议问题