繁体   English   中英

打印输出到同一行

[英]Getting print output onto the same line

我正在研究网络自动化工具。 它的物流运作良好。 该脚本将打开一个文本文件,其中包含要连接的设备列表。 当脚本连接到设备时,它会打印出脚本的各个阶段。 问题是设备的输出与设备本身位于不同的线路上。

在文本文件中,我有这些名字:

device1
device2
device3

当脚本运行时,输出如下:

09:57:13: device1

Checking this device for available space 

09:57:15: device1

 Sufficient space available 

09:57:19: device2

 Checking this device for available space 
09:57:21: device2

 Sufficient space available 

09:57:25: device3 Checking this device for available space 

09:57:27: device3 Sufficient space available 

基本上我希望所有输出看起来都像他们为 device3 所做的那样。 但与此同时,我希望文本文件能够让人们轻松地将他们的设备列表粘贴到其中。

脚本部分粘贴如下:

with open('devices.txt', 'r') as routers:
    contents = routers.readlines()
    for host in contents:
#for device_type in device_types:
        global hostname
        u = 'autouser-netmri'
        p = 'tRB66J2S'
        host = host
        cisco = {
            'device_type': 'cisco_ios',
            'host': host,
            'username': 'autouser-netmri',#u,  # ssh username
            'password': 'tRB66J2S',#p,  # ssh password
        }

        now = datetime.now()
        logs_time = now.strftime("%H:%M:%S")
        print("" + logs_time + ": " + host + " Checking this device for available space ")

在每一行的末尾有一个换行符\\n并且 print 也会添加一个。

您应该从文件中读取的文本中删除换行符

host.strip()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM