Python a bytes-like object is required, not 'str'
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/twisted/python/log.py", line 103, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/local/lib/python3.5/dist-packages/twisted/python/log.py", line 86, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/local/lib/python3.5/dist-packages/twisted/python/context.py", line 122, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/local/lib/python3.5/dist-packages/twisted/python/context.py", line 85, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/local/lib/python3.5/dist-packages/twisted/internet/posixbase.py", line 614, in _doReadOrWrite
why = selectable.doRead()
File "/usr/local/lib/python3.5/dist-packages/twisted/internet/tcp.py", line 243, in doRead
return self._dataReceived(data)
File "/usr/local/lib/python3.5/dist-packages/twisted/internet/tcp.py", line 249, in _dataReceived
rval = self.protocol.dataReceived(data)
File "main.py", line 32, in dataReceived
data = redis_protocol.decode(rcvdata) # data类型:list
File "/usr/local/lib/python3.5/dist-packages/redis_protocol/protocol.py", line 22, in decode
processed, index = 0, data.find(DELIMITER)
builtins.TypeError: a bytes-like object is required, not 'str'
python2转python3遇到的问题
看源码
data = redis_protocol.decode(rcvdata)
command = " ".join(redis_protocol.decode(rcvdata))
rcvdata在python2中为str类型,但是在python3中为bytes类型,所以我们要转换参数类型
data = redis_protocol.decode(rcvdata.decode()) # data类型:list
command = " ".join(redis_protocol.decode(rcvdata.decode())) # command类型:string
转换方法
从str到bytes:调用方法encode().
从bytes到str:调用方法decode().
相关文章
- Visual Studio Code is not showing the ouput of Python
- 虚拟环境运行python代码报错/lib64/libstdc++.so.6: version `GLIBCXX_3.4.20‘ not found
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
- Python 包含文件夹路径转字典
- Python解析url、提取url参数、提取url数据(Python2、Python3、提取url、端口、协议、路径)
- Python name ‘basestring‘ is not defined
- 【零基础学python】:清华官方出品的《看漫画学Python》全彩PDF,495页资源分享
- 【Python】自动化抢勾,python软件安装教程
- python中if x 和if x is not None的区别用法详解
- python安装whl包时出现的问题解决is not a supported wheel on this platform
- Python——pip安装报错:is not a supported wheel on this platform
- gyp ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- python搭建简单http服务
- 《Python和Pygame游戏开发指南》——2.13 Rect对象
- 《像计算机科学家一样思考Python》——3.6 定义和使用
- Python爬虫教程之五大重要库入门使用教程Scrapy、Requests、Urllib、Beautiful Soup、Selenium(教程含源码)
- Python 常用的ORM框架简介
- 转 Python多版本管理-pyenv
- 【Python分布式服务框架】python实现gRPC服务
- Python Matplotlib.plot Update image Questions
- 机器学习算法与Python实践之(三)支持向量机(SVM)进阶
- python 逻辑运算符 () > not > and > or
- odoo14 与 odoo13 多对多的赋值不同 Python TypeError:‘int‘ object is not subscriptable
- python 报错 name is not defined
- 【python 学习】——pycharm终端解释器和Python解释器配置
- 【一文读懂】python 中的 numpy.reshape(a, newshape, order=‘C‘) 详细说明及实例讲解
- 【Linux学习笔记】解决:yum install python-dev 报错(CentOS安装Python-dev库)
- python算法之组合总和