解决python3下读取redis返回byte类型的问题

我们首先查看redis连接的构造方法

    def __init__(self, host='localhost', port=6379,
                 db=0, password=None, socket_timeout=None,
                 socket_connect_timeout=None,
                 socket_keepalive=None, socket_keepalive_options=None,
                 connection_pool=None, unix_socket_path=None,
                 encoding='utf-8', encoding_errors='strict',
                 charset=None, errors=None,
                 decode_responses=False, retry_on_timeout=False,
                 ssl=False, ssl_keyfile=None, ssl_certfile=None,
                 ssl_cert_reqs=None, ssl_ca_certs=None,
                 max_connections=None):

decode_responses=fase这个参数决定返回结果是否decode,所以只需要设置连接将其改为true即可

sr = StrictRedis(decode_responses=True)
    原文作者:Chen_Yakun
    原文地址: https://segmentfault.com/a/1190000018685475
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞