Python - a bytes like object is required, not str
Asked Answered
U

1

62

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.

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
Unclassical answered 15/4, 2015 at 6:59 Comment(1)
See also: Ned Batcheler's Pragmatic Unicode, or, How Do I Stop the Pain?Grocery
G
99

try

data = twitchdata.decode().split(":")[1]

instead of

data = twitchdata.split(":")[1]
Giuseppe answered 15/4, 2015 at 7:3 Comment(5)
I think you mean decode. And of course the same would be needed for the other split calls, so the decoding should happen once, in the first line. Plus the question if ASCII is even the right encoding...Grocery
I think is only decode() and I think is related to docs.python.org/release/3.0.1/whatsnew/…Giuseppe
Thank you. I used data = twitchdata.decode("ascii").split(":")[1] and that workedUnclassical
I thing is better without ascii, it will free your code from issues with unicodeGiuseppe
I ve solved the same issue with bytes(my_str, encoding='UTF-8') and removing everything that was left for PY2 compatibility.Blucher

© 2022 - 2024 — McMap. All rights reserved.