-3

So i'm a streamer trying to make a twitch plays mod, and im struggling with one error to fix. The original source code was written in python 2.7, but im trying to do this in python 3.11. The code is:

    if self.check_has_message(data):
        return [self.parse_message(line) for line in filter(None, data.split('\r\n'))];

gave error code:

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

I tried doing:

if self.check_has_message(data):

  return [self.parse_message(line) for line in filter(None, data.decode('utf-8').split('\r\n'))];

did not work.

I have no idea what im doing with this tbh but any help is great!

Traceback (most recent call last):

File "D:\twitchplays\wituz\main.py", line 18, in new_messages = t.twitch_recieve_messages(); ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\twitchplays\wituz\twitch.py", line 73, in twitch_recieve_messages return [self.parse_message(line) for line in filter(None, data.split('\r\n'))]; ^^^^^^^^^^^^^^^^^^ TypeError: a bytes-like object is required, not 'str'

Silph48
  • 1
  • 3
  • Did you google the error message: `typeerror: a bytes-like object is required, not 'str'`? This is a FAQ. Your question suggests you have made no effort to solve it. It's great that you recognize you have no idea what you're doing but we still expect you to make some effort to solve it yourself. Such as by doing the most basic research. – Kurtis Rader Aug 11 '23 at 04:02
  • Try `b'\r\n'`, might work. – Mark Ransom Aug 11 '23 at 04:03
  • @KurtisRader i did actually do research on google, but to no avail with this error. Ive been testing different things for an hour but still not getting anything – Silph48 Aug 11 '23 at 04:07
  • @MarkRansom I tried and it gave some new error code that google just says im opening the file wrong (im not opening any fille) if self.check_has_message(data): return [self.parse_message(line) for line in filter(None, data.decode('utf-8').split(b'\r\n'))] – Silph48 Aug 11 '23 at 04:15
  • @Silph48 does this answer your question? https://stackoverflow.com/questions/33054527/typeerror-a-bytes-like-object-is-required-not-str-when-handling-file-conte – Suraj Shourie Aug 11 '23 at 04:19
  • @SurajShourie not really. I have no indication of what part i need to convert to or from binary, ive tried line, filter, data, and what mark said but nothing is working – Silph48 Aug 11 '23 at 04:21
  • Does the traceback really implicate the line of code you put in your question? Because I don't see anything about that line that would cause that error. Also, are you really sure you want to split on the string `\r\n`? That won't split if only a newline is present. Presumably it is the `self.parse_message()` method that is causing the error because `data.split()` wouldn't cause it. – Kurtis Rader Aug 11 '23 at 04:27
  • i can copy the whole traceback. i just put it on the bottom up there. is there another option besides splitting the data here? – Silph48 Aug 11 '23 at 04:29
  • *"is there another option besides splitting the data here?"* - You haven't provided enough context for anyone to answer that. My advice would be to use the python debugger to figure out exactly what is required to be "a bytes-like object". Understanding the problem will help you work out how to solve it. – Stephen C Aug 11 '23 at 04:38
  • @StephenC what sort of context is needed here to help show the answer? – Silph48 Aug 11 '23 at 04:42
  • 1
    Read the rest of my comment. Also re: "I have no idea what I'm doing with this": you should probably working on >that< problem. – Stephen C Aug 11 '23 at 04:42
  • @StephenC im trying to read this but i have to be honest no idea what any of this means, im not an expert in python and its just tracebacks and stuff that ive never seen. Im sorry but like i dont know this- – Silph48 Aug 11 '23 at 04:46
  • @KurtisRader its showing with the red arrows that the data.split is whats causing it. – Silph48 Aug 11 '23 at 04:47
  • That's what I am trying to say ... you should be working on your python expertise ... so that you can solve this kind of problem for yourself. As it is, the only way someone could help you is if you sent them your entire application, input data, etc so that they could *reproduce the error* for themselves. That is most likely impractical. – Stephen C Aug 11 '23 at 04:49
  • @StephenC do you reccomend i just run it in 2.7 then if i cant fix it this way? – Silph48 Aug 11 '23 at 04:51
  • I'm not recommending anything. Apart from what I've already said. – Stephen C Aug 11 '23 at 04:52
  • "im trying to read this but i have to be honest no idea what any of this means, im not an expert in python and its just tracebacks and stuff that ive never seen. Im sorry but like i dont know this" - then you should *follow a tutorial from the beginning and properly learn Python*, rather than trying to struggle with a project like this. Adapting legacy code from Python 2.x, in general, is an advanced skill that requires a superior understanding. You need to first develop at least basic [debugging](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) skills. – Karl Knechtel Aug 11 '23 at 06:26
  • If you are confronted with a line of code that has multiple things in it that you don't understand, you should first try to break the logic apart into separate steps. At the very least, try to *test* what each part does in isolation. For example: can you find a way to *find out* what the value of `data` is, *before* this line of code? *What happens* if you try just the `data.split('\r\n')` part by itself? – Karl Knechtel Aug 11 '23 at 06:28

1 Answers1

0

Please, learn how to format a literal block. See Cannot split, a bytes-like object is required, not 'str'. The error message is misleading because you are using a string to split a byte array. Change the '\r\n' to b'\r\n'.

>>> b"a\nb".split("\n")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
>>> b"a\nb".split(b"\n")
[b'a', b'b']

I also recommend learning to add print statements at strategic points in your code to understand what type of object is being operated on. For example:

>>> print(b"a\nb")
b'a\nb'
>>> print("a\nb")
a
b
>>> print(type("a\nb"))
<class 'str'>
>>> print(type(b"a\nb"))
<class 'bytes'>
Kurtis Rader
  • 6,734
  • 13
  • 20
  • Holy cow thank you so much! self.parse_message(line.decode('utf-8'')) and that fixed it – Silph48 Aug 11 '23 at 05:25
  • 2
    Please read [answer] and note well that this is **not a discussion forum**. If you find a duplicate link, vote to close the question as a duplicate - don't write an answer and cite the duplicate. If there are formatting issues, edit the question yourself if you can fix them, and otherwise comment to ask OP to do it, and/or vote to close as unclear. Meta commentary does not belong in answers. – Karl Knechtel Aug 11 '23 at 06:23