print输出回显是一大片空白,但是你将结果write写进txt文本时,是有内容的,但是每行代码之间会有2行空白,不美观。
result = stdout.read() 显示的是bytes字节字符串:
b"\r\r\n\x00******************************************************************************\r\r\n* Copyright (c) 2004-2017
为了将字节字符串变成人可读的文本,得decode('utf-8')转码变成str类型字符串,但是控制台回显是空白。
'\r'是回到本行首部,'\n'是换行,所以解法是利用str.replace()方法,将'\r'替换为空字符串''
result = stdout.read().decode('utf-8').replace('\r','')
此时输出结果如下:
******************************************************************************
* Copyright (c) 2004-2017 New H3C
Technologies Co., Ltd. All rights reserved.*
* Without the owner's prior written
consent,*
* no decompiling or reverse-engineering
shall be allowed.*
******************************************************************************
dis cur
#
version
7.1.075, Alpha 7571
#
这时,内容全部正常,但是有h3c的广告,我不喜欢,就利用切片把h3c恶心的广告去掉:
result = stdout.read().decode('utf-8').replace('\r','').split('\n')
fori inresult[7:]:
print(i)
最终的结果完美输出,无广告:
C:\Users\admin\PycharmProjects\venv\Scripts\python.exe
C:/Users/admin/PycharmProjects/ssh.py
dis cur
#
version 7.1.075, Alpha 7571
#
sysname H3C
#
irf
mac-address persistent timer