[英]Python 3 - Formatting print commands
python 新手,我正在嘗試從網絡設備導出一些信息並將其格式化為文本文件。 我正在運行以下代碼;
for device in (LD6AGGSW01 , LD6AGGSW02 ):
net_connect = ConnectHandler(**device)
output = net_connect.send_command('show hostname')
pickle.dump(output, open("hostname.p", "wb"))
output = net_connect.send_command('sh ver | i NXOS | i image |i file |i is:')
output = output.strip()
pickle.dump(output, open("IOSimage.p", "wb"))
output = pickle.load(open("hostname.p","rb"))
print('Hostname: ' + output)
output = pickle.load(open("IOSimage.p","rb"))
print(output)
我試圖改變輸出看起來像:
Hostname: LD6-AGG-SW01
NXOS image file is: bootflash:///nxos.7.0.3.I3.1.bin
Hostname: LD6-AGG-SW02
NXOS image file is: bootflash:///nxos.7.0.3.I3.1.bin
到:
Hostname: LD6-AGG-SW01
NXOS image file is: bootflash:///nxos.7.0.3.I3.1.bin
Hostname: LD6-AGG-SW02
NXOS image file is: bootflash:///nxos.7.0.3.I3.1.bin
關於我將如何處理這件事的任何想法?
我假設net_connect.send_command()
返回一個帶有尾隨換行符的字符串。
如果這是正確的,
for device in (LD6AGGSW01 , LD6AGGSW02 ):
net_connect = ConnectHandler(**device)
hostname = net_connect.send_command('show hostname').strip()
ios_image = net_connect.send_command('sh ver | i NXOS | i image |i file |i is:').strip()
print('Hostname:', hostname)
print('Image:', ios_image)
print()
– 即strip()
輸出 – 應該可以解決問題。
使用.strip()
刪除輸出中不需要的\\n
字符:
for device in (LD6AGGSW01 , LD6AGGSW02 ):
net_connect = ConnectHandler(**device)
output = net_connect.send_command('show hostname')
pickle.dump(output, open("hostname.p", "wb"))
output = net_connect.send_command('sh ver | i NXOS | i image |i file |i is:')
output = output.strip()
pickle.dump(output, open("IOSimage.p", "wb"))
output = pickle.load(open("hostname.p","rb"))
output = output.strip() # Strip output
print('Hostname: ' + output)
output = pickle.load(open("IOSimage.p","rb"))
output = output.strip() # Strip output
print(output)
print() # Call print function to insert extra line
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.