Python3 reading redis data with 'b'
#encoding=utf-8 from redis import * #Read the data d1=input("The data you have entered is:") #Connect r=StrictRedis(host='localhost',port=6379) #Write # pipe=() # ('r1','hello') # ('r2','world') # () #Read # In front of python3 there is a 'b' for bytes #Solve with #Note that values are encoded when available #Fa1: # temp=('r1').decode() # print(temp) #Fa 2: # temp=('r2') # h1=str(temp,encoding='utf-8') # print(h1) #None if there is no value s=(d1) print(s) if s==None: print('yes') else: print('no') temp=() print(temp)
base code
from redis import * if __name__ == '__main__': sr = StrictRedis(host='localhost', port=6379, db=0) result=('name','python') print(result) result1 = ('name') print(result1)
Run results:
True
b'python'
The data we put in is of string type, but the data we take out is of byte type, this is due to the problem of python3's driver that interacts with redis, Python2 takes out the string type.
In order to get a string type of data, you can take it out and decode it each time, but it's too cumbersome to set it up like this:
sr = StrictRedis(host='localhost', port=6379, db=0,decode_responses=True)
I.e. just add decode_responses=True when connecting to the database
Python how to remove data taken out of redis b
Solution 1
To convert a link to a string type, use the following command
()
Solution 2
str(temp,encoding='utf-8')
Solution Three
Set it when connecting to redis to avoid frequent conversion operations
StrictRedis(host='localhost', port=6379, db=0,decode_responses=True)
rationale
Python3 has problems interacting with redis on the driver, which is not the case if you use python2.
Similarly in python3 print data starting with b' represents bytes type data.
This issue must be kept in mind to avoid spending more time troubleshooting problems when the program is making judgments.
summarize
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.