Python - 需要像对象这样的字节,而不是 str
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29643544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:49:34 来源:igfitidea点击:
Python - a bytes like object is required, not str
提问by spencermehta
I'm moving my Twitch bot from Python 2.7 to Python 3.5. I keep getting the error:
a bytes like object is required not 'str'
on the 2nd line of the code below.
我正在将我的 Twitch 机器人从 Python 2.7 迁移到 Python 3.5。我不断收到错误消息:
a bytes like object is required not 'str'
在下面代码的第二行。
twitchdata = irc.recv(1204)
data = twitchdata.split(":")[1]
twitchuser = data.split("!")[0]
twitchmsg = twitchdata.split(":")[2]
chat = str(twitchuser) +": "+ str(twitchmsg)
print(chat) #prints chat to console
回答by valentin
try
尝试
data = twitchdata.decode().split(":")[1]
instead of
代替
data = twitchdata.split(":")[1]