Python3 .replace产生TypeError:需要一个类似字节的对象,而不是'str'

劳伦室

我一直在尝试使用以下代码从服务器读取输出:

s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname, port, username, password)
command = 'xe vm-list'
(stdin, stdout, stderr) = s.exec_command(command)

output = stdout.read()
x = output.replace("\n", ",").strip()
print(x)
s.close()

当运行“ x = output.replace(“ \ n”,“,”)。strip()“行时,” TypeError:需要一个类似字节的对象,而不是'str'“。

我究竟做错了什么?

欧拜汉

您必须解码bytes对象以获取字符串。去做这个:

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

在其中用您的远程计算机的编码替换UTF-8。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python3 TypeError:需要一个类似字节的对象,而不是“str”

TypeError:在Python3中写入文件时需要一个类似字节的对象,而不是'str'

Python 3-TypeError:需要一个类似字节的对象,而不是'str'

Python 2到3“ TypeError:需要一个类似字节的对象,而不是'str'”

Python 3,TypeError:需要一个类似字节的对象,而不是'str'

Python3写入gzip文件-memoryview:需要一个类似字节的对象,而不是'str'

使用Python 3.5,抛出错误:TypeError:需要一个类似字节的对象,而不是'str'

TypeError:需要一个类似字节的对象,而不是'str'–用Python保存JSON数据

Python 3.7 TypeError:需要一个类似字节的对象,而不是'str'

python 3.5:“ TypeError:memoryview:需要一个类似字节的对象,而不是'str'”

TypeError:需要一个类似字节的对象,而在python和CSV中不是'str'

TypeError:需要一个类似字节的对象,而在python和CSV中不是'str'

Python错误:TypeError:需要一个类似字节的对象,而不是'str'

TypeError:需要一个类似字节的对象,而在Python 3中打开Python 2 Pickle文件时不是'str'

类型错误:需要一个类似字节的对象,而不是无服务器和 Python3 的“str”

Python 3 升级,需要一个类似字节的对象,而不是“str”

Python 3.5 TypeError:需要一个类似字节的对象,而不是使用truncate过滤器的'str'

Python 3.6:TypeError:在尝试打印页面中的所有链接时,需要一个类似字节的对象,而不是'str'

TypeError:在Python 3.5.1中使用REST时需要一个类似字节的对象,而不是'str'

需要一个类似字节的对象,而不是python中的“ str”错误

使用python计算excel中的重复行,并且我收到错误TypeError:需要一个类似字节的对象,而不是'str'

python3:类型错误:需要类似字节的对象,而不是“str”

Python:需要一个类似字节的对象,打印时不需要'str'

需要一个类似字节的对象,而不是'str'

错误:需要一个类似字节的对象,而不是'str'

TypeError:需要一个类似字节的对象,而不是'str''xxx';'xxx'

如何解决Json错误:TypeError:需要一个类似字节的对象,而不是'str'

子进程“ TypeError:需要一个类似字节的对象,而不是'str'”

TypeError:需要一个类似字节的对象,而不是从文件中读取时的“ str”