pexpect can give you Unicode strings, unless you ask it to give you bytes.
If you want it to give you bytes—e.g., because you don't know the encoding the telnet server is expecting you to use—that's fine, but then you have to deal with it as bytes. That means using bytes patterns, not string patterns, in re:
for eol in [b'\r\n', b'\r', b'\n']:
content = re.sub(b'%s$' % eol, b'', content)
But if you didn't want bytes, it's better to get everything decoded to str, and the your existing code would just work:
content = pexpect.run('ls -l', encoding='utf-8')
for eol in ['\r\n', '\r', '\n']:
content = re.sub('%s$' % eol, '', content)
As a side note, if you're just trying to remove a final newline on the last line, it's a lot easier to do that without a regex:
content = content.rstrip('\r\n')
Or, if you're trying to do something different, like remove blank lines, even that might be better written explicitly:
content = '\n'.join(line for line in content.splitlines() if line)
… but that still leaves you with the same problem of needing to use b'\n' or '\n' appropriately, of course.
contentis abytesor otherbytes-like object. Since you haven't shown us what it is or where it comes from, it's hard to say more. Maybe the problem is that you calledencodesomewhere you shouldn't have, or you're using arequestsresponse and usedr.contentinstead ofr.text, or you opened a file in binary mode instead of text mode. Or maybe you just need to calldecodeoncontextwith the right encoding. Or maybe it really should just be abytes, and you just need to use abytespattern, so change all those string literals into bytes literals.contentcome from, what encoding is it in, is it supposed to be abytes, what do you want to do with it, etc.—so instead of giving you a half-dozen options of which none may be relevant to you, we can actually give you an answer.bytes, and even edited that into the question. Why are you asking him to check it?