Python typeError: a bytes-like object is required, not ‘str’ Solution

简介: Python typeError: a bytes-like object is required, not ‘str’ Solution

Python typeError: a bytes-like object is required, not ‘str’ Solution


目录

一、需求

二、报错

三、解决方法

一、需求

调接口解析其中 dis 字段。

二、报错

Python Typeerror a bytes-like object is required not ‘str’

这句话的意思是“类型错误:需要类似字节的对象,而不是字符串”。

三、解决方法

在需要解析的字段前 加上 b

原代码:

if 'dis' in response:
    dis = response.split('<dis>')[1]

现代码:

if b'dis' in response:
    dis = response.split(b'<dis>')[1]
相关文章
|
8月前
|
索引 Python
Python错误 - 'list' object is not callable 的问题定位与解决
出现编程问题并不可怕,关键在于是否可以从中学习与成长。遇到'list' object is not callable这样的错误,我们不仅需要学会应对,更需要了解其背后的原因,避免类似的问题再次出现。记住,Python的强大功能和灵活性同时也意味着我们需要对其理解更准确,才能更好的使用它。
966 70
|
C++ Python
Python Tricks--- Object Comparisons:“is” vs “==”
Python Tricks--- Object Comparisons:“is” vs “==”
118 1
|
JSON 安全 数据格式
7-6|python报错TypeError: can't pickle _thread.RLock objects
7-6|python报错TypeError: can't pickle _thread.RLock objects
|
数据处理 Python
【Python】解决tqdm ‘module‘ object is not callable
在使用tqdm库时遇到的“'module' object is not callable”错误,并给出了正确的导入方式以及一些使用tqdm的常见示例。
600 1
|
JSON 数据格式 Python
【python】解决json.dump(字典)时报错Object of type ‘float32‘ is not JSON serializable
在使用json.dump时遇到的“Object of type ‘float32’ is not JSON serializable”错误的方法,通过自定义一个JSON编码器类来处理NumPy类型的数据。
760 1
|
Python
python类型错误(TypeError)
【7月更文挑战第13天】
574 9
|
Python
python类型错误(TypeError)
【7月更文挑战第20天】
456 4
|
API C++ Python
【Azure 应用服务】Python fastapi Function在Azure中遇见AttributeError异常(AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async')
【Azure 应用服务】Python fastapi Function在Azure中遇见AttributeError异常(AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async')
177 0
|
TensorFlow API 算法框架/工具
【Tensorflow+keras】解决使用model.load_weights时报错 ‘str‘ object has no attribute ‘decode‘
python 3.6,Tensorflow 2.0,在使用Tensorflow 的keras API,加载权重模型时,报错’str’ object has no attribute ‘decode’
255 0

推荐镜像

更多