关于Python3.4.1客户端连接redis中的char b前缀 [英] About char b prefix in Python3.4.1 client connect to redis

查看:62
本文介绍了关于Python3.4.1客户端连接redis中的char b前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了麻烦.我的代码如下.但我不知道为什么在输出字符串Hello Python"之前有一个字符b".

<预><代码>>>>导入redis>>>redisClient = redis.StrictRedis(host='192.168.3.88',port=6379)>>>redisClient.set('test_redis', 'Hello Python')真的>>>value = redisClient.get('test_redis')>>>打印(值)b'Hello Python'//为什么是 char 'b' 输出?

解决方案

b'Hello Python' 是一个字节字符串——redis 会在你输入的时候自动编码一个 unicode 字符串,但是你的工作是在出路时解码它.

最好明确编码和解码:

<预><代码>>>>redisClient.set('test_redis', 'Hello Python'.encode('utf-8'))>>>redisClient.get('test_redis').decode('utf-8')'你好蟒蛇'

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?

解决方案

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'

这篇关于关于Python3.4.1客户端连接redis中的char b前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆