Python 3.x Error:TypeError: a bytes-like object is required, not 'str'
2016-11-05 21:32
513 查看
TypeError: a bytes-like object is required, not 'str'
问题分析
该问题主要是由于当前操作的字符串是bytes类型的字符串对象,并对该bytes类型的字符串对象进行按照str类型的操作。如下面图所示,s 为bytes类型字符串对象。
当对s进行按照str类型的操作(split)时,会弹出一下错误提示。因为split函数传入的参数是str类型的字符串,而党鞭的s为bytes编码类型的字符串对象
解决方法
此时的解决办法是,将s转码成为str类型的或者是将对s进行str类型操作转化为对bytes类型的操作。方法1:bytes-->str
方法2:对s进行str类型操作转化为对bytes类型的操作
由于split里面用到分解字符串中的是空格‘ ’,但是该‘ ’是按照str编码的,需要转化成为bytes类型的,然后再直接对s进行对bytes类型的操作。
str和bytes类型之间的常用转码方式:
1、str to bytes:(3种方式)2、bytes to str (3种方式)
相关文章推荐
- Python 3.x: TypeError: a bytes-like object is required, not 'str'
- python3中,socket使用send函数时出现的错误:TypeError: a bytes-like object is required, not 'str'
- python中Base64编码和解码:TypeError: a bytes-like object is required, not 'str'
- python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file
- Python中3+版本运行2+版本程序出现TypeError: a bytes-like object is required, not 'str'之读取PDF中的图片
- Python 3.5 Socket TypeError: a bytes-like object is required, not 'str' 错误提示
- python socket send 函数 报错:TypeError: a bytes-like object is required, not 'str'
- python3.5 模拟UDP客户端发送数据报文,报错:TypeError: a bytes-like object is required, not 'str'
- python write()参数错误,导致报错TypeError: a bytes-like object is required, not 'str'
- Python学习中出现TpyeErrora bytes-like object is required, not 'str'
- TypeError: a bytes-like object is required, not 'str'
- TypeError: a bytes-like object is required, not 'str'
- 【Python】TypeError: a bytes-like object is required, not 'str'解决
- 【Python】TypeError: a bytes-like object is required, not 'str'解决
- Python中“a bytes-like object is required, not 'str'”的问题
- python问题:TypeError: a bytes-like object is required, not 'str'
- python 3.5: TypeError: a bytes-like object is required, not 'str'
- python3 TypeError: a bytes-like object is required, not 'str'
- linux下运行python3出现TypeError: a bytes-like object is required, not 'str'
- 解决python3的pickle.load错误:a bytes-like object is required, not 'str'