Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python3/lib/python3.4/site-packages/redis/client.py", line 863, in get
return self.execute_command('GET', name)
File "/usr/local/python3/lib/python3.4/site-packages/redis_py_cluster-1.0.0-py3.4.egg/rediscluster/utils.py", line 85, in inner
_keys = set(sum([list(obj.keys()) for obj in objs],[]))
File "/usr/local/python3/lib/python3.4/site-packages/redis_py_cluster-1.0.0-py3.4.egg/rediscluster/client.py", line 293, in execute_command
File "/usr/local/python3/lib/python3.4/site-packages/redis/client.py", line 577, in parse_response
response = connection.read_response()
File "/usr/local/python3/lib/python3.4/site-packages/redis/connection.py", line 569, in read_response
response = self._parser.read_response()
File "/usr/local/python3/lib/python3.4/site-packages/redis/connection.py", line 266, in read_response
response = response.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
复制代码
>>> from redis import StrictRedis
>>> r = StrictRedis('localhost', 6380)
>>> r.set('hello', '你好')
True
>>> r.get('hello')
b'\xe4\xbd\xa0\xe5\xa5\xbd'
复制代码
能够看见,字符串输入被编码成utf8存储在Redis里了。而取出来的时候仍是被编码后的bytes
,须要显示的decode
才能变成字符串。python
Redis创建链接时有两个参数,一个是encoding
指定编码,默认是utf8
。一个是decode_responses
,默认为False
,若是是True
,会以encoding
方式解码,而后返回字符串。若是是字符串,就根据encoding
编码成bytes
。redis
>>> from redis import StrictRedis
>>> r = StrictRedis('localhost', 6380, encoding='utf8', decode_responses=True)
>>> r.set('hello', '你好')
True
>>> r.get('hello')
'你好'
复制代码
依然报上面的错编码
链接 redis 时带上 decode_responses=False 取消对返回结果的 decode('utf-8') 操做spa
>>import redis
>>import gzip
>>rdb = redis.Redis(host='127.0.0.1',6379,db=0,decode_responses=False)
>>rdb.set('test.zip',gzip.compress(b'xxxxxxxxxxxxx'))
>>rdb.get('test.zip')
b'\x1f\x8b\x08\x00\xd5;\xe1V\x02\xff\xab\xa8@\x02\x00\xd6\x82q\xe4\r\x00\x00\x00'
复制代码
带来的弊端的返回的结果是byte类型,要自行decodecode
网上还有人推荐修改源码,我没尝试过,直接粘贴过来的 ,但也是一种方案嘛。ip
位置在 python安装目录/lib/python3.4/site-packages/redis/connection.py 266行 注:能够先将egg包解压,而后将egg包更个名 如此未用gzip压缩的返回的是utf-8,而通过gzip压缩过的返回的是byteutf-8
elif byte == '+':
pass
# int value
elif byte == ':':
response = long(response)
# bulk response
elif byte == '$':
length = int(response)
if length == -1:
return None
response = self._buffer.read(length)
# multi-bulk response
elif byte == '*':
length = int(response)
if length == -1:
return None
response = [self.read_response() for i in xrange(length)]
if isinstance(response, bytes) and self.encoding:
#此处加个异常处理就能够了
try:
response = response.decode()
except :
pass
return response
复制代码
>>import redis
>>import gzip
>>rdb = redis.Redis(host='127.0.0.1',6379,db=0,decode_responses=False)
>>rdb.set('test.zip',gzip.compress(b'xxxxxxxxxxxxx'))
>>rdb.get('test.zip')
b'\x1f\x8b\x08\x00\xd5;\xe1V\x02\xff\xab\xa8@\x02\x00\xd6\x82q\xe4\r\x00\x00\x00'
>>rdb.set('test.str','xxxx')
>>rdb.get('test.str')
'xxxx'
复制代码