python - 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: a bytes-like object is required, not 'str'”。
我究竟做错了什么?
解决方案
您必须解码字节对象以获取字符串。去做这个:
output = stdout.read().decode("UTF-8")
在那里用远程机器的编码替换 UTF-8。
推荐阅读
- ruby-on-rails - Rails 的 localhost :“缺少请求格式的模板”?
- mysql - mysql中Rails的类似沙盒的功能
- c++ - 如果使用自定义模板类,则对基类一无所知
- sql - 什么是最有效的方法来找到获取所有符合特定标准的记录的“逆”
- tensorflow - 将 TensorBoard 2 中的 2 个图与 TensorFlow 2 合并
- apache-camel - 收到事件后是否可以读取文件?
- android - 删除领域中的重复项
- java - 想在 Selenium 中创建一个计时器并执行一些操作,直到达到定义的时间段
- batch-file - Windows 10 下对命令提示符标题行的奇怪更改
- vue.js - 如何从指令 Vue.js 调用元素的方法并传递参数?