我刚开始在python、命令行和jupyter笔记本中使用redis,我在这两个地方都得到了附加的文本。代码如下:
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??对于存储在redis中的正确值和所有内容,它的工作方式与您所期望的一样,只是不确定为什么b显示在文本的开头
发布于 2017-07-19 10:58:51
结果它不是一个字符串,而是一个字节字符串结果,'b‘表示这一点。您可以通过armnotstrong What does the 'b' character do in front of a string literal?提供的链接了解更多信息
但我在这篇文章中找到了答案About char b prefix in Python3.4.1 client connect to redis
要获得字符串形式的结果,您有两种选择:
字符集1.设置连接中的编码(=‘utf-8’,decode_responses=True)
或
2.对结果进行解码以转换为字符串,例如r.get('foo').decode('utf-8')
https://stackoverflow.com/questions/45179985
复制相似问题