print输出有空行,怎么删除

代码如下: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

中间有几行空行,怎样才能把输出的空行删除,连续输出

只删除空行,其他格式保持不变

print(Cmd_out) 之前判断下 Cmd_out是不是空字符串即可

    if Cmd_out.strip(' \r\n')!="":
        print(Cmd_out)

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

print前判断一下Cmd_out是否为空啊

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632

直接:

img