0

I just started to use redis with python, from command line and in jupyter notebooks and i get appended text to both places. code is as follows:

import redis
r = redis.StrictRedis(host='123.456.789.123', port=6379, db=0, password='mypwd')

r.hmset('hmfoo', { 'name': 'myname', 'username': 'myusername'})
True

r.hget('hmfoo', 'name')
b'myname' <----- why is the 'b' appended here??

it works as you expect with the right values stored in redis and everything, just not sure why the b is showing at the start of the text

CaptRisky
  • 751
  • 4
  • 13
  • 25
  • great link, also this one gave me the clear answer i needed https://stackoverflow.com/questions/25745053/about-char-b-prefix-in-python3-4-1-client-connect-to-redis – CaptRisky Jul 19 '17 at 02:52

1 Answers1

1

it turns out its not a string but a byte string result and the 'b' is to indicate that. You can learn more about it from the link provided by armnotstrong What does the 'b' character do in front of a string literal?

but i found the answer in this SO post About char b prefix in Python3.4.1 client connect to redis

to get the result as a string you have 2 options:

1. set the encoding in the connection (charset='utf-8', decode_responses=True)

or

2. decode the result to convert to a string for example r.get('foo').decode('utf-8')

CaptRisky
  • 751
  • 4
  • 13
  • 25