56

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
>>> redisClient = redis.StrictRedis(host='192.168.3.88',port=6379)
>>> redisClient.set('test_redis', 'Hello Python')
True
>>> value = redisClient.get('test_redis')
>>> print(value)
b'Hello Python' //why char 'b' output?
Eric
  • 95,302
  • 53
  • 242
  • 374
Zhi Su
  • 573
  • 1
  • 4
  • 4

2 Answers2

102

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.

Eric
  • 95,302
  • 53
  • 242
  • 374
mickeyandkaka
  • 1,452
  • 2
  • 11
  • 21
  • 9
    Note that this prevents you from storing true binary data in your database, as it will be incorrectly treated as UTF8-encoded strings – Eric Sep 09 '14 at 12:51
  • 2
    Odd how `decode_responses` is left undescribed in [the docs](http://redis-py.readthedocs.org/en/latest/#redis.StrictRedis) – Eric Sep 09 '14 at 12:52
  • @Eric the defination is `class redis.StrictRedis(host='localhost', port=6379, db=0, password=None, socket_timeout=None, connection_pool=None, charset='utf-8', errors='strict', decode_responses=False, unix_socket_path=None)` , `decode_responses=False` is default. – mickeyandkaka Sep 09 '14 at 12:55
  • 1
    What does the `port=6379` parameter do? – ChaimG Jul 16 '17 at 16:38
  • @CaptRisky, obviously. But why should I care which port is being used; it seems to work fine when I leave out this arg? I checked the redis-py docs and didn't find anything about this. – ChaimG Jul 19 '17 at 16:38
  • If it's omitted, it uses the default port for redis, if you configured redis for a different port, you would require this parameter – CaptRisky Jul 19 '17 at 16:51
  • Thanks. I used `redis_db = redis.Redis(host='localhost', port=6379, db='12',decode_responses=True, charset='utf-8')`, and worked. – Mohsen Mar 05 '18 at 07:32
  • @Eric: true binary data can still be stored (and will be so correctly); in order to retrieve such binary data, another client needs to be used. I proposed something like that [here](https://stackoverflow.com/q/63164404/758174). – Pierre D Jul 30 '20 at 00:31
61

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'
Eric
  • 95,302
  • 53
  • 242
  • 374
  • Thank you so much for this...but does it seem logically correct that it will auto-encode on its own will and but decode only if you say it to..I mean does not seem more of a self biased way of getting things done. – siddharthrc Mar 08 '18 at 10:50
  • @sidd.rc: One of the key ideas in python 3 is that encoding/decoding is never automatic. auto-encoding here is very bad, because you can't decode it unless you know which encoding was used. – Eric Mar 08 '18 at 11:08
  • @Eric ok thank you for this :) , I am new to python myself . Much thanks for adding to my knowledge – siddharthrc Mar 08 '18 at 11:28