陈超
AttributeError: \'NoneType\' object has no attribute \'encode\'
2021-3-4 15:50
阅读:9339

AttributeError: 'NoneType' object has no attribute 'encode'

解析:'NoneType' object 即空值,null 对象,这个对象是没有属性的。所以,后面调用这个属性的时候,就会报错。

解决方法:用if - else  语句处理容易返回none的数据;或者用异常处理。



def _write(self, data: str):

    """Sends command to sensor."""

    self._serial.write(data.encode())


改为

def _write(self, data: str):

    """Sends command to sensor."""

    if data : self._serial.write(data.encode())


转载本文请联系原作者获取授权,同时请注明本文来自陈超科学网博客。

链接地址:https://wap.sciencenet.cn/blog-858128-1274995.html?mobile=1

收藏

分享到:

当前推荐数:0
推荐到博客首页
网友评论0 条评论
确定删除指定的回复吗?
确定删除本博文吗?