Skip to content Skip to sidebar Skip to footer

Python3 .replace Yielding Typeerror: A Bytes-like Object Is Required, Not 'str'

I've been trying to read the output from a server with this code: s = paramiko.SSHClient() s.load_system_host_keys() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.conne

Solution 1:

You have to decode bytes object to get string. To do this:

output = stdout.read().decode("UTF-8")

in there replace UTF-8 with the encoding of your remote machine.

Solution 2:

output is a bytes object, not str. You need to pass its replace method bytes as well, in this case by adding a b prefix to the literals:

x = output.replace(b"\n", b",").strip()

Post a Comment for "Python3 .replace Yielding Typeerror: A Bytes-like Object Is Required, Not 'str'"