前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 与 redis 交互取出数据类型是 bytes 时,如何直接取出 str 数据类型

Python 与 redis 交互取出数据类型是 bytes 时,如何直接取出 str 数据类型

作者头像
卓越笔记
发布2023-02-18 10:42:08
6060
发布2023-02-18 10:42:08
举报
文章被收录于专栏:卓越笔记卓越笔记

运行代码:(存 str 类型,得 bytes 类型,这是由于 Python3 与 redis 交互的驱动的问题,Python2 取出来的就是 str 类型的)

代码语言:javascript
复制
[root@VM_2_29_centos ~]# workon
blog
Joyo
testdata
[root@VM_2_29_centos ~]# workon Joyo
(Joyo) [root@VM_2_29_centos ~]# python
[root@VM_2_29_centos ~]# python3
Python 3.6.4 (default, Mar 16 2018, 22:27:18) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from redis import *
>>> sr = StrictRedis(host='localhost', port=6379, db=0)
>>> sr
StrictRedis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
>>> res = sr.set("name", "python")
>>> print(res)
True
>>> res1 = sr.get("name")
>>> print(res1)
b'python'
>>>

修改连接配置参数即可直接得到 str :(增加 decode_responses=True)

代码语言:javascript
复制
>>> sr1 = StrictRedis(host='localhost', port=6379, db=0, decode_responses=True)
>>> sr1
StrictRedis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
>>> res2 = sr1.set("name3","python3")
>>> print(res2)
True
>>> res3 = sr1.get("name3")
>>> print(res3)
python3
>>>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-10-2 0,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档