我正在使用Python 3.我編寫了兩個程序。通過csv文件循環獲取思科交換機的IP地址,登錄,運行命令,並將結果輸出到單個文本文件。所以我最終得到了許多文本文件......每個開關一個。第二個程序使用xlwt將每個文本文件中的信息寫入Excel中自己的工作表。刪除兩個單詞之間的所有字符並用空格替換
主要想法是我需要開發一個顯示端口進出服務的報告。一旦我將這些導入到Excel中,我可以編寫一些公式來提取我需要的數據。但現在,當我將它導入Excel時,我必須手動刪除一些單元格,因爲所有內容都不對齊,這是因爲名稱列中某些單詞之間的空格(我正在導入到Excel中作爲空間分隔)。我試圖用字符串和列表方法(分割,連接,切片等)做一些事情,但我無法得到我想要的。名稱列在任何一致的約定中都沒有標準化。我注意到,雖然名稱實際上可能非常長,但會被截斷爲一定數量的字符。理想情況下,刪除前4行(最上面有空行)和最後一行,然後在端口和狀態之間任意刪除它(全部刪除包括標題的列)。
這是從交換機獲取數據後的文件顯示方式。
sw1#term length 0 sw1#show interfaces status Port Name Status Vlan Duplex Speed Type Gi0/1 Trunk to switch (a connected 1 a-full a-100 10/100/1000BaseTX Gi0/2 Network augment pe connected 1 a-full a-1000 10/100/1000BaseTX Gi0/3 connected 1 a-full a-1000 10/100/1000BaseTX Gi0/4 connected 1 a-full a-100 10/100/1000BaseTX Gi0/5 notconnect 1 auto auto Not Present Gi0/6 notconnect 1 auto auto Not Present Gi0/7 notconnect 1 auto auto Not Present Gi0/8 notconnect 1 auto auto Not Present Gi0/9 notconnect 1 auto auto Not Present Gi0/10 connected 1 a-full a-100 10/100/1000BaseTX Gi0/11 notconnect 1 auto auto Not Present Gi0/12 connected 1 a-full a-100 10/100/1000BaseTX Gi0/13 disabled 1 auto auto Not Present Gi0/14 disabled 1 auto auto Not Present Gi0/15 disabled 1 auto auto Not Present Gi0/16 disabled 1 auto auto Not Present sw1#logout
最終結果我想在下面。這應該允許行/列結構在導入Excel時保持不變。請注意,所有列信息均以空格分隔。我發現導入爲固定寬度或用空格分隔,連續空格作爲一個對象進行檢查似乎工作得很好。
Port Status Vlan Duplex Speed Type Gi0/1 connected 1 a-full a-100 10/100/1000BaseTX Gi0/2 connected 1 a-full a-1000 10/100/1000BaseTX Gi0/3 connected 1 a-full a-1000 10/100/1000BaseTX Gi0/4 connected 1 a-full a-100 10/100/1000BaseTX Gi0/5 notconnect 1 auto auto Not Present Gi0/6 notconnect 1 auto auto Not Present Gi0/7 notconnect 1 auto auto Not Present Gi0/8 notconnect 1 auto auto Not Present Gi0/9 notconnect 1 auto auto Not Present Gi0/10 connected 1 a-full a-100 10/100/1000BaseTX Gi0/11 notconnect 1 auto auto Not Present Gi0/12 connected 1 a-full a-100 10/100/1000BaseTX Gi0/13 disabled 1 auto auto Not Present Gi0/14 disabled 1 auto auto Not Present Gi0/15 disabled 1 auto auto Not Present Gi0/16 disabled 1 auto auto Not Present
任何指針,將不勝感激。我認爲正則表達式可能是有序的,但我需要一些幫助來構建它。我希望這不是太模糊。
除去以前的更新並移動到一個新的線程
後的代碼你有這麼遠。最初的想法是'split()'完全有能力處理這個;跳過索引'0'後的所有內容直到出現「連接|未連接|禁止」字樣;之後,一切看起來相當符合我... –