a bytes-like object is required, not 'str'

a bytes-like object is required, not 'str',这个问题怎么办

原代码是

img


然后出现了错误a bytes-like object is required, not 'str'

网友说加encode(),

img

然后问题变成了AttributeError: 'bytes' object has no attribute 'encode'
然后我又改成decode()

img

然后错误又变成了AttributeError: 'str' object has no attribute 'decode'

这是为什么?

a bytes-like object is required, not 'str',这个问题出现的代码是哪一行,看起来不像是list1这一行的错误啊,完整报错内容贴一下看看

单重语法看没啥问题,把代码贴全一些
(实践一下,以免某老师又来一句,不要相信权威,盲人摸太阳)

img

问题的关键在于 inputstring 这个变量是什么类型?如果是字节流类型,像代码中这样使用replace方法,就会报错 a bytes-like object is required, not 'str'
但是字节流类型的变量应该拥有decode方法,而当你使用decode后,却提示没有这个方法,推测有可能是inputstring是在循环中使用的变量,它的值在字符串str和字节流bytes之间变换。
最好能看到更多的代码才能判断。