代码如下:import subprocess
p = subprocess.Popen('ping baidu.com',
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, )
while True:
result = p.stdout.readline()
if result != b'':
try:
Cmd_out = str(result.decode('gbk').strip(' \r\n'))
except:
Cmd_out = str(result.decode('utf-8').strip(' \r\n'))
else:
break
keyword = Cmd_out.find('来自') # 行是否有关键字‘来自’
if keyword != -1:
Cmd_out = '\b'
print(Cmd_out)
输出结果:
正在 Ping baidu.com [220.181.38.148] 具有 32 字节的数据:
220.181.38.148 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 56ms,最长 = 57ms,平均 = 56ms
中间有几行空行,怎样才能把输出的空行删除,连续输出
只删除空行,其他格式保持不变